- 一、 v-for 中的 Ref 数组
- 二、插槽统一
- 三、attribute 强制行为
- 四、 $attrs 包含 class & style
- 五、自定义指令
- 六、与自定义元素的互操作性
- 七、Data 选项
- 八、函数式组件
- 九、全局 API
- 十、 key Attribute
- 十一、按键修饰符
- 十二、在 prop 的默认函数中访问 this
- 十三、渲染函数 API
- 十四、transition 组件过渡的 class 名更改
- 十五、Transition 作为根节点
- 十六、Transition Group 根元素
- 十七、v-model
- 十八、v-if 与 v-for 的优先级对比
- 十九、v-bind 合并行为
- 二十、VNode 生命周期事件名
- 二十一、侦听数组
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
二、插槽统一
移除 this.$scopedSlots
将所有 this.$scopedSlots
替换为 this.$slots
将所有 this.$slots.mySlot
替换为 this.$slots.mySlot()
1. Vue 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
元素渲染方面
<template slot="t1" slot-scope="scope">
<div>
具名插槽 t1,数据{{scope.data}}
</div>
</template>
<!-- 或者 v-slot -->
<template v-slot:t1="scope"> <!-- 简写:#t1="scope" -->
<div>
具名插槽 t1,数据{{scope.data}}
</div>
</template>
2. Vue 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()
元素渲染方面
移除 slot
和 slot-scope
,仅仅保留使用 v-slot
<template v-slot:t1="scope"> <!-- 简写:#t1="scope" -->
<div>
具名插槽 t1,数据{{scope.data}}
</div>
</template>
具名插槽
# 子组件
<div>
<slot name="header"></slot>
<slot></slot>
<slot name="footer"></slot>
</div>
# 父组件
<Dialog>
<template v-slot:header>
<div>1</div>
</template>
<template v-slot>
<div>2</div>
</template>
<template v-slot:footer>
<div>3</div>
</template>
</Dialog>
作用域插槽
在子组件动态绑定参数 派发给父组件的 slot 去使用
<div>
<slot name="header"></slot>
<div>
<div v-for="item in 100">
<slot :data="item"></slot>
</div>
</div>
<slot name="footer"></slot>
</div>
父组件结构取值
<Dialog>
<template #header>
<div>1</div>
</template>
<template #default="{ data }">
<div>{{ data }}</div>
</template>
<template #footer>
<div>3</div>
</template>
</Dialog>
动态插槽
插槽可以是一个变量名
<template>
<Dialog>
<template #[name]>
<div>
23
</div>
</template>
</Dialog>
</template>
<script setup>
const name = ref('header')
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论