随笔2:作用域插槽

作用域插槽

子组件通过插槽将数据传给父组件。
父组件通过slot-scope或者v-slot接收一个子组件传递的Object数据,key为prop名称,value为prop的值。

父组件:

<template>
  <div>
    <CardList>
      <template slot="cardList" slot-scope="cardData">
      	<h4>{{cardData.otherData}}</h4>
        <ul>
          <li v-for="item in cardData.list">
          	{{item}}
	      </li>
        </ul>
      </template>
    </CardList>
    <CardList>
      <template v-slot:cardList="cardData">
        <h4>{{cardData.otherData}}</h4>
        <ul>
          <li v-for="item in cardData.list">
            {{item}}
          </li>
        </ul>
      </template>
    </CardList>
  </div>
</template>

<script>
  import CardList from "./slotTest/CardList";

  export default {
    name: 'HelloWorld',
    components: {CardList}
  }
</script>

子组件:

<template>
  <div class="slot-test">
    <h3>我是卡片列表组件</h3>
    <slot name="cardList" :list="list" :otherData="'这是标题数据'"/>
  </div>
</template>

<script>
  export default {
    name: "CardList",
    props: {
    },
    data() {
      return {
        list: [
          {name: '卡1', content: '这是卡1'},
          {name: '卡2', content: '这是卡2'},
          {name: '卡3', content: '这是卡3'}
        ]
      }
    }
  }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值