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修饰符:
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">` }){{ myText }}
递归组件、组件别名 例如:名为 为了避免组件名和变量冲突,可以使用组件别名 命名空间组件(引入多个组件时非常方便): 使用自定义指令: 这里有一个需要注意的限制:必须以 在 使用 为了在 在 ... 单文件组件的 SFC
.sync修饰符的替换: vue3中的v-if比v-for的优先级高,与vue2相反 $listeners 被移除或整合到 $attrs $attrs 现在包含 class 和 style attribute ... --FooBar.vue
的组件可以在其模板中用
引用它自己。import { FooBar as FooBarChild } from './components'
vNameOfDirective
的形式来命名本地自定义指令,以使得它们可以直接在模板中使用。
This is a Heading
defineProps
和 defineEmits
中必须使用
defineProps
和 defineEmits
API 来声明 props
和 emits
,它们具备完整的类型推断并且在 中是直接可用的:
defineExpose
的组件是默认关闭的,也即通过模板 ref 或者
$parent
链获取到的组件的公开实例,不会暴露任何在 中声明的绑定。
组件中明确要暴露出去的属性,使用
defineExpose
编译器宏:useSlots
和 useAttrs
使用
slots
和 attrs
的情况应该是很罕见的,因为可以在模板中通过 $slots
和 $attrs
来访问它们。在你的确需要使用它们的罕见场景中,可以分别用 useSlots
和 useAttrs
两个辅助函数:useSlots
和 useAttrs
是真实的运行时函数,它会返回与 setupContext.slots
和 setupContext.attrs
等价的值,同样也能在普通的组合式 API 中使用与普通的
一起使用
顶层
await
中可以使用顶层
await
。结果代码会被编译成 async setup()
:状态驱动的动态 CSS
标签可以通过
v-bind
这一 CSS 函数将 CSS 的值关联到动态的组件状态上:
的key用该设置在template标签上
v-bind="object"
和一个相同的独立 attribute覆盖可以控制,写在后面的覆盖前面的