Vue3升级了哪些重要的新功能

343 阅读1分钟
  • createApp (Vue.createApp(App))
  • emits属性 (用作声明, 和prop类似, 声明组件会触发这个方法)
  • 多事件处理 (一次性写多个事件。@click="handleA, handleB")
  • Fragment (组件不需要有一个根元素了)
<template>
    <div></div>
    <div></div>
</template>
  • 移除 sync改为v-model参数
// vue2
<A :visible.sync="a"/>
// vue3
<A v-model:visible="a"/>

// A 组件
this.$emit(‘update:visible’, false)
  • 异步组件的引用方式
// vue2
()=>import('component')

// vue3
defineAsyncComponent(()=>import('component'))

  • 移除filter
  • teleport
// 把组件A渲染到body
<teleport to="body"> 
    <A/>
</teleport>
  • suspense
<suspense>
    <template><A/></template> // 异步组件加载前会显示下面的loading...
    <template #fallback>loading...</template>
</suspense>

composition Api

只讲新增加重要的

  • reactive
  • ref、toRef、toRefs
  • readonly
  • watch、watchEffect
  • useSlots (获取slots)
  • defineEmits (定义事件)
  • defineProps (定义prop)
  • defineExpose (定义外部通过实例可用的方法和属性)