vue3新特性


--

teleport可以指定元素渲染到某一元素下;

如:

<teleport to="body">
  <div>父级teleportdiv>
teleport>

emits自定义事件

emits: ['inFocus', 'submit'] // 会覆盖原生事件
emits: {
    // 没有验证
    click: null,

    // 验证 submit 事件
    submit: ({ email, password }) => {
      if (email && password) {
        return true
      } else {
        console.warn('Invalid submit event payload!')
        return false
      }
    }
  }

v-module可以绑定多个值

name
  v-model:first-name="firstName"
  v-model:last-name="lastName"
>

自定义v-model修饰符:

{{ myText }}
app.component('my-component', { props: { modelValue: String, modelModifiers: { default: () => ({}) } }, emits: ['update:modelValue'], methods: { emitValue(e) { let value = e.target.value if (this.modelModifiers.capitalize) { value = value.charAt(0).toUpperCase() + value.slice(1) } this.$emit('update:modelValue', value) } }, template: `<input type="text" :value="modelValue" @input="emitValue">` })

递归组件、组件别名

例如:名为 FooBar.vue 的组件可以在其模板中用  引用它自己。

为了避免组件名和变量冲突,可以使用组件别名

import { FooBar as FooBarChild } from './components'

命名空间组件(引入多个组件时非常方便):



使用自定义指令:

这里有一个需要注意的限制:必须以 vNameOfDirective 的形式来命名本地自定义指令,以使得它们可以直接在模板中使用。


defineProps 和 defineEmits

在 

defineExpose

使用 

useSlots 和 useAttrs

在 

useSlots 和 useAttrs 是真实的运行时函数,它会返回与 setupContext.slots 和 setupContext.attrs 等价的值,同样也能在普通的组合式 API 中使用

与普通的 

状态驱动的动态 CSS

单文件组件的 

SFC

 .sync修饰符的替换