【vue】插槽

插槽
    插槽内容
    编译作用域
        父级模板里的所有内容都是在父级作用域中编译的;子模板里的所有内容都是在子作用域中编译的。
    后备内容
        为插槽设置默认值。
    具名插槽
            //子组件中定义插槽
            <slot name="header"></slot>
            //父组件中传递插槽内容
            <template v-slot:header>
                <h1>Here might be a page title</h1>
            </template>

            没有写在具名插槽中的内容,默认传递到默认插槽中。
            默认插槽定义    <slot></slot>
            默认插槽传递内容 <template v-slot:default>内容</template>
    作用域插槽
        父组件插槽作用域使用子组件的对象
            <template v-slot:default="slotProps">
                {{slotProps.user.firstName}}
            </template>
        独占默认插槽的缩写语法:当被提供的内容只有默认插槽时,组件的标签才可以被当作插槽的模板来使用。
        <current-user v-slot="slotProps">
          {{ slotProps.user.firstName }}
        </current-user>
        解构插槽
        <current-user v-slot='{user}'>
            {{user.firstName}}
        </current-user>
        解构时设置默认值
        <current-user v-slot="{user = {firstName:'jack'}}">
            {{user.firstName}}
        </current-user>
        动态插槽名
        <base-layout>
          <template v-slot:[dynamicSlotName]>
            ...
          </template>
        </base-layout>
        具名插槽的缩写
        v-slot: 可以缩写为 #
        v-slot与slot,slot-scope的区别

        //v-slot
        //子组件暴露插槽变量
        <template>
            <slot name="column1" :row="{id:1, name:'jack'}">
        </template>
        //使用子组件插槽的变量
        <template v-slot:column1="row">
            {{row.name}}
        </template>

        //slot,slot-scope
        //子组件暴露插槽变量
        <template>
            <slot name="column1" :row="{id:1, name:'jack'}">
        </template>
        //使用子组件插槽的变量
        <template slot="column1" slot-scope="row">
            {{row.name}}
        </template>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值