Vue 3.x 插槽统一

发布于 2025-03-11 09:51:32 字数 1611 浏览 6 评论 0

此更改统一了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

冬天旳寂寞

暂无简介

文章
评论
29 人气
更多

推荐作者

颉】

文章 0 评论 0

hiasdf

文章 0 评论 0

过期以后

文章 0 评论 0

悲歌长辞

文章 0 评论 0

梦归所梦

文章 0 评论 0

梦境

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文