插槽二(默认,具名,作用域)

插槽

默认插槽

BiliBili.vue

 <div class="aside-wrap">
	 <h2>排行榜</h2>
	 <!-- 插槽 -->
	 <slot></slot>
 </div>

App.vue

 <BiliBili>
        <ul>
            <li v-for="(aw,index) in awList" :key="index">
                <span>{{index + 1}}</span> 
                {{aw.title}}
            </li>
        </ul>  
  </BiliBili>
    ------------------------------
    data(){
    return{
      awList:[
          {title:'大家好我是李毅大帝,今天我入驻',url:'http://www.baidu.com'},
          {title:'震惊,半夜为何无故被敲门',url:'http://www.baidu.com'},
          {title:'震惊国人的10件事,尤其是第七个。',url:'http://www.baidu.com'},
          {title:'多活10年,不看后悔。',url:'http://www.baidu.com'},
          {title:'美国沉默,完全颠覆你的想象。',url:'http://www.baidu.com'},
          {title:'果断出手,这简直太神了。',url:'http://www.baidu.com'},
          {title:'灭绝人性,真是太天才了',url:'http://www.baidu.com'},
          {title:'国人不忍了,难以置信的骗术',url:'http://www.baidu.com'},
          {title:'美国慌了,不分享还是人吗?',url:'http://www.baidu.com'}
      ],

具名插槽(添加name属性,防止重名问题)

BiliBili.vue

 <div class="aside-wrap">
    <h2>
         <slot name="awname"></slot>排行榜
     </h2>
     <!-- 具名插槽 -->
     <slot name="aw"></slot>
</div>

App.vue

 <BiliBili>
        <!-- template专门用来渲染使用的,不会在页面显示 -->
        <template slot="awname">动漫</template>

        <ul slot="aw">
            <li v-for="(aw,index) in awList" :key="index">
                <span>{{index + 1}}</span> 
                {{aw.title}}
            </li>
        </ul>  
    </BiliBili>
    -----------------------------------------------------------
    data(){
    return{
      awList:[
          {title:'大家好我是李毅大帝,今天我入驻',url:'http://www.baidu.com'},
          {title:'震惊,半夜为何无故被敲门',url:'http://www.baidu.com'},
          {title:'震惊国人的10件事,尤其是第七个。',url:'http://www.baidu.com'},
          {title:'多活10年,不看后悔。',url:'http://www.baidu.com'},
          {title:'美国沉默,完全颠覆你的想象。',url:'http://www.baidu.com'},
          {title:'果断出手,这简直太神了。',url:'http://www.baidu.com'},
          {title:'灭绝人性,真是太天才了',url:'http://www.baidu.com'},
          {title:'国人不忍了,难以置信的骗术',url:'http://www.baidu.com'},
          {title:'美国慌了,不分享还是人吗?',url:'http://www.baidu.com'}
      ],

作用域插槽(添加 slot-scope属性)

BiliBili.vue

 <div class="aside-wrap">
     <h2>
            <slot name="awname"></slot>排行榜
        </h2>
        <!-- 作用域插槽 -->
        <slot name="aw" :awList="awList"></slot>
  </div>
  --------------------------------------------------
    data(){
        return {
             awList:[
                {title:'大家好我是李毅大帝,今天我入驻',url:'http://www.baidu.com'},
                {title:'震惊,半夜为何无故被敲门',url:'http://www.baidu.com'},
                {title:'震惊国人的10件事,尤其是第七个。',url:'http://www.baidu.com'},
                {title:'多活10年,不看后悔。',url:'http://www.baidu.com'},
                {title:'美国沉默,完全颠覆你的想象。',url:'http://www.baidu.com'},
                {title:'果断出手,这简直太神了。',url:'http://www.baidu.com'},
                {title:'灭绝人性,真是太天才了',url:'http://www.baidu.com'},
                {title:'国人不忍了,难以置信的骗术',url:'http://www.baidu.com'},
                {title:'美国慌了,不分享还是人吗?',url:'http://www.baidu.com'}
            ],

App.vue

<BiliBili>
 <!-- template专门用来渲染使用的,不会在页面显示 -->
    <template slot="awname">动漫</template>

    <ul slot="aw" slot-scope="{awList}">
        <li v-for="(aw,index) in awList" :key="index">
            <span>{{index + 1}}</span> 
            {{aw.title}}
        </li>
    </ul>  
</BiliBili>
### Vue3 具名作用域插槽的使用方法 在 Vue3 中,具名作用域插槽是一种强大的功能,允许子组件向父组件传递数据的同时指定特定位置的内容。通过 `v-slot` 指令及其简写形式 `#`,可以在模板中定义具名作用域插槽。 #### 子组件 (ChildComponent.vue) 以下是子组件的一个简单实现,其中包含了两个具名作用域插槽: ```vue <template> <div class="child-component"> <!-- 默认插槽 --> <slot></slot> <!-- 具名作用域插槽 --> <slot name="header" :message="headerMessage"></slot> <!-- 另一个具名作用域插槽 --> <slot name="footer" :info="footerInfo"></slot> </div> </template> <script> export default { data() { return { headerMessage: '这是头部消息', footerInfo: { text: '这是底部信息', timestamp: new Date().toLocaleString() } }; } }; </script> ``` 在此示例中,子组件提供了三个插槽默认插槽、名为 `header` 的具名作用域插槽以及名为 `footer` 的另一个具名作用域插槽[^3]。 #### 父组件 (ParentComponent.vue) 接下来,在父组件中可以通过 `v-slot` 或其简写语法绑定到这些插槽并访问它们的作用域变量。 ```vue <template> <div class="parent-component"> <h2>父组件内容</h2> <!-- 使用 ChildComponent 并填充插槽 --> <ChildComponent> <!-- 默认插槽 --> 这是默认插槽的内容。 <!-- 具名作用域插槽 - Header --> <template #header="{ message }"> <p style="color: blue;">{{ message }}</p> </template> <!-- 具名作用域插槽 - Footer --> <template v-slot:footer="scopeProps"> <p style="color: green;"> {{ scopeProps.info.text }} | 时间戳: {{ scopeProps.info.timestamp }} </p> </template> </ChildComponent> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent } }; </script> ``` 在这个例子中,父组件利用了两种方式来声明具名作用域插槽: 1. **简写形式** (`#`) 对于 `header` 插槽; 2. **完整指令形式** (`v-slot:`) 对于 `footer` 插槽[^4]。 两者都可以正常工作,并且可以根据个人偏好选择其中之一。 --- ### 总结 Vue3 提供了一种更加直观的方式来操作具名作用域插槽,使得父子组件之间的交互变得更加灵活和清晰。上述代码展示了如何创建具有多个具名作用域插槽的子组件,以及如何在父组件中正确地消费这些插槽的数据[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值