学习Vue3 第十七章(插槽slot)

插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签。

匿名插槽

1.在子组件放置一个插槽

<template>
    <div>
       <slot></slot>
    </div>
</template>

父组件使用插槽

在父组件给这个插槽填充内容

        <Dialog>
           <template v-slot>
               <div>2132</div>
           </template>
        </Dialog>

具名插槽·

具名插槽其实就是给插槽取个名字。一个子组件可以放多个插槽,而且可以放在不同的地方,而父组件填充内容时,可以根据这个名字把内容填充到对应插槽中

    <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>

 插槽简写

        <Dialog>
            <template #header>
               <div>1</div>
           </template>
           <template #default>
               <div>2</div>
           </template>
           <template #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>

 

动态插槽

插槽可以是一个变量名

        <Dialog>
            <template #[name]>
                <div>
                    23
                </div>
            </template>
        </Dialog>
const name = ref('header')
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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小满zs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值