vue之组件插槽slot、作用域插槽

父组件决定子组件显示子组件数据

const app = Vue.createApp({
    template:`
    <div>
        <mylist v-slot="slotProps">
            <ul>
                <li>{{ slotProps.item }}</li>
            </ul>
        </mylist>

    </div>
    `,
});
app.component('mylist',{
    data(){
        return{
            list:[1,2,3,4,5]
        }
    },
    template:`
     <slot v-for="item in list" :item="item"></slot>
    `
})

或者

const app = Vue.createApp({
    template:`
    <div>
        <mylist v-slot="item">
            <ul>
                <li>{{ item }}</li>
            </ul>
        </mylist>

    </div>
    `,
});
app.component('mylist',{
    data(){
        return{
            list:[1,2,3,4,5]
        }
    },
    template:`
     <slot v-for="item in list" :item="item"></slot>
    `
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中的作用域插槽是一种功能强大的特性,它允许你在父组件中向子组件传递内容,并且还能让子组件对这些内容进行处理和渲染。 使用作用域插槽,你可以在子组件中定义插槽的具体渲染规则,并且可以在父组件中通过具名插槽的方式向子组件传递内容。子组件可以通过`v-slot`指令来接收父组件传递的内容,并在插槽内部进行渲染。 在Vue 3中,作用域插槽的语法有所改变。你可以使用`<slot>`元素加上`name`属性来定义具名插槽,在父组件中使用`v-slot`指令来向该具名插槽传递内容。例如: ```html <!-- 子组件 --> <template> <div> <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </div> </template> <!-- 父组件 --> <template> <div> <child-component> <template v-slot:header> <!-- 这里的内容将会传递给子组件的具名插槽header --> </template> <!-- 这里的内容将会传递给子组件的默认插槽 --> <template v-slot:footer> <!-- 这里的内容将会传递给子组件的具名插槽footer --> </template> </child-component> </div> </template> ``` 在Vue 3中,还支持使用缩写语法来定义默认插槽和具名插槽。例如,`<template v-slot:header>`可以简写为`<template #header>`,`<template v-slot:footer>`可以简写为`<template #footer>`。 通过使用作用域插槽,你可以更灵活地向子组件传递内容,并让子组件有更大的自由度来处理和渲染这些内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值