作用域插槽复习

案例描述:循环显示个人信息,在图片一列,因为有可能显示图片链接或是图片,所以我们采用插槽来实现。

涉及到的技术点:父传子通信技术、作用域插槽(在父组件中可获取到子组件中的变量)

实现代码:

父组件UseTable.vue

<template>
  <div>
    <my-table :arr="list">
        <template v-slot="scope">
          <a :href="scope.row.headImgUrl">{{ scope.row.headImgUrl }}</a>
        </template>
    </my-table>
    <!-- 显示图片 -->
    <my-table :arr="list">
      <template v-slot="scope"> 
          <img style="width:100px" :src="scope.row.headImgUrl" alt="">
      </template>
    </my-table>
  </div>
</template>

<script>
import MyTable from '../components/04/MyTable.vue'
export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [
        {
          name: "小传同学",
          age: 18,
          headImgUrl:
            "http://yun.itheima.com/Upload/./Images/20210303/603f2d2153241.jpg",
        },
        {
          name: "小黑同学",
          age: 25,
          headImgUrl:
            "http://yun.itheima.com/Upload/./Images/20210304/6040b101a18ef.jpg",
        },
        {
          name: "智慧同学",
          age: 21,
          headImgUrl:
            "http://yun.itheima.com/Upload/./Images/20210302/603e0142e535f.jpg",
        },
      ],
    };
  },
};
</script>

<style>
</style>

 子组件:MyTable.vue

<template>
  <div>
      <table border="1">
          <thead>
              <tr>
                  <th>序号</th>
                  <th>姓名</th>
                  <th>年龄</th>
                  <th>头像</th>
              </tr>
          </thead>
          <tbody>
              <tr v-for="(obj, index) in arr" :key="index">
                  <td>{{ index + 1 }}</td>
                  <td>{{ obj.name }}</td>
                  <td>{{ obj.age }}</td>
                  <td>
                      <slot :row="obj">
                          <!-- 默认值给上,如果使用组件不自定义标签显示默认文字 -->
                          {{ obj.headImgUrl}}
                      </slot>
                  </td>
              </tr>
          </tbody>
      </table>
  </div>
</template>

<script>
export default {
    props: {
        arr: Array
    }
}
</script>

小结: 插槽可以自定义标签, 作用域插槽可以把组件内的值取出来自定义内容

项目中具名插槽的应用

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
插槽作用域是指在Vue组件中,插槽可以接受来自父组件的数据,并在模板中使用这些数据。通过插槽作用域,我们可以将父组件的数据传递给插槽内部,实现数据的动态渲染。 作用域插槽是指在定义插槽时,可以将插槽对应的数据传递给子组件,并在子组件中根据该数据进行渲染。作用域插槽通过在插槽中添加一个属性,将父组件的数据传递到插槽内部,然后在子组件中使用这个属性来渲染内容。这样,我们可以在父组件中动态地向子组件插入不同的内容。 总结起来,插槽作用域作用域插槽是Vue组件中的两个重要概念。插槽作用域允许父组件向插槽传递数据,而作用域插槽则允许子组件接收并使用这些数据来渲染内容。这样的设计使得组件之间的数据传递更加灵活和可控。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue匿名插槽作用域插槽的合并和覆盖行为](https://download.csdn.net/download/weixin_38659622/12943692)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Vue作用域插槽实现方法及作用详解](https://download.csdn.net/download/weixin_38641150/12925203)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [浅析vue插槽作用域插槽的理解](https://download.csdn.net/download/weixin_38743372/12943719)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值