Vue 3.x 插槽统一
此更改统一了 3.x 中的普通插槽和作用域插槽。
以下是变化的变更总结:
this.$slots
现在将插槽作为函数公开- 非兼容 :移除
this.$scopedSlots
请继续阅读来获取更多信息!
2.x 语法
当使用渲染函数,即 h
时,2.x 曾经在内容节点上定义 slot
数据 property。
// 2.x 语法
h(LayoutComponent, [
h('div', { slot: 'header' }, this.header),
h('div', { slot: 'content' }, this.content)
])
此外,可以使用以下语法引用作用域插槽:
// 2.x 语法
this.$scopedSlots.header
Normal slots are rendered during the parent’s render cycle. When a dependency of a slot changes, it causes both the parent and child components to re-render. Scoped slots, on the other hand, are compiled into inline functions and called during the child component’s render cycle. This means any data dependencies relied on by a scoped slot are collected by the child component, resulting in more precise updates. In 2.6, we have introduced an optimization that further ensures parent scope dependency mutations only affect the parent and would no longer force the child component to update if it uses only scoped slots
3.x 语法
在 3.x 中,插槽以对象的形式定义为当前节点的子节点:
// 3.x Syntax
h(LayoutComponent, {}, {
header: () => h('div', this.header),
content: () => h('div', this.content)
})
当你需要以编程方式引用作用域插槽时,它们现在被统一到 $slots
选项中了。
// 2.x 语法
this.$scopedSlots.header
// 3.x 语法
this.$slots.header()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: Vue 3.x 异步组件
下一篇: 数据库知识点分享
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论