Vue $slots 对象
示例
使用 $slots
对象来检查父组件是否提供了 'topSlot' 的内容。
mounted(){
if(this.$slots.topSlot){
this.slotsText = "Content for the 'topSlot' slot is provided by the parent."
}
else {
this.slotsText = "Content for the 'topSlot' slot is NOT provided by the parent."
}
}
运行示例 »
定义和用法
$slots
对象表示从父组件传递过来的插槽。
可以通过调用 this.$slots
来查看从父组件传递过来的插槽,或者调用 this.$slots.topSlot
来查看名为 'topSlot' 的特定插槽。
$slots
对象可以用于检查父组件是否提供了插槽,就像上面的示例一样,或者用于编写渲染函数。
相关页面
Vue 教程:Vue 插槽
Vue 教程:作用域插槽
Vue 教程:Vue v-slot
Vue 参考:Vue v-slot 指令