v-slot
限制:
<template>
标签中 父组件:<templaye> <p>abc</p> </templaye>
子组件:<templaye> <slot></slot> </templaye>
插槽默认值
插槽可以使用默认值<slot>default</slot>
,若父组件不提供任何模板,将渲染为默认值default,如果提供模板,那会把default替换掉
v-slot:header(简写#header)
(Vue2.6.11版本前的写法:slot="slotProps")
Parent
<template v-slot:header>
<p>NameSlot</p>
</template>
child
<template>
<slot name="header"></slot>
</template>
<!- -接收 prop 的具名插槽- ->
v-slot:header="slotProps"(简写:#header="slotProps")
(Vue2.6.11版本前的写法:slot-scop="slotProps")
<child-component>
<template #headers="slotProps">
//1、所有子组件插槽传过来的数据(:headers="layout")都会被当成一个数据对象(键值:headers)存放到slotProps对象中
2、引用:slotProps.headers.header
<p>{{slotProps.headers.header}}</p>
</template>
</child-component>
子组件
Vue.component('child',{
data () {
return {
layout: {
header: '作用域插槽'
}
}
},
template:
`<div>
<slot name="headers" :headers="layout"></slot> <!-- 默认插槽,作用域插槽 -->
</div>`
})
常用场景:
父组件(不同或单个)对子组件处理后的数据有不同的操作,渲染方式,这时就需要用到作用域插槽。
封装组件(如:列表)
1.父组件传递数据给子组件
2.子组件处理应用数据
3.子组件通过 <slot name="child-one" :todo="todo">
的方式,把新数据传递给父组件
4.父组件通过v-slot:child-one="slotProps"
的方式,接受todo对象,之后通过slotProps.todo.xxx
的方式使
有时候使用封装的组件后,需要拿到封装的组件渲染后的数据进行更多的逻辑操作
因篇幅问题不能全部显示,请点此查看更多更全内容
怀疑对方AI换脸可以让对方摁鼻子 真人摁下去鼻子会变形
女子野生动物园下车狼悄悄靠近 后车司机按喇叭提醒
睡前玩8分钟手机身体兴奋1小时 还可能让你“变丑”
惊蛰为啥吃梨?倒春寒来不来就看惊蛰
男子高速犯困开智能驾驶出事故 60万刚买的奔驰严重损毁