• 作用域插槽


    子组件
    <template>
      <div class="list">
        <div class="list-title">{{title}}</div>
        <ul class="list-content">
          <li v-for="(item ,index) in content" :key="index">
            <!--这里将content中的每一项数据绑定到slot的item变量上,在父组件中可以获取到item变量-->
            <slot :item="item">{{item}}</slot>
          </li>
        </ul>
      </div>
    </template>
     
    
    

    父组件  

    <template>
      <div>
        <MyList title="标题1" :content="listData1"></MyList>
        <MyList title="标题2" :content="listData2">
          <template slot-scope="scope">   // 为了修改原有子组件的代码,相当于重新写子组件,用slot-scope 可以使父组件获取到数据后修改
            <img :src="scope.item.img" width="20">
            <span>{{scope.item.text}}</span>
          </template>
        </MyList>
        <MyList title="标题3" :content="listData3">
          <template slot-scope="scope">
            <b>{{scope.item.prefix ? '有前缀' : '无前缀'}}</b>
            <span>{{scope.item.text}}</span>
            <span>{{scope.item.remark}}</span>
          </template>
        </MyList>
      </div>
    </template>
    
    <script>
      import myList from './List.vue';
    
      export default {
        name: 'HelloWorld',
        components: {
          'MyList': myList
        },
        data() {
          return {
            listData1: [
              '列表项1',
              '列表项2',
              '列表项3'
            ],
            listData2: [
              {text: '第二个列表的列表项1', img: 'example.png'},
              {text: '第二个列表的列表项2', img: 'example.png'},
              {text: '第二个列表的列表项3', img: 'example.png'}
            ],
            listData3: [
              {text: '第三个列表的列表项1', prefix: true, remark: '附加的备注1'},
              {text: '第三个列表的列表项2', prefix: false, remark: '附加的备注2'},
              {text: '第三个列表的列表项3', prefix: true, remark: '附加的备注3'}
            ],
          }
        }
      }
    </script>
    

      

  • 相关阅读:
    红黑树
    Spring事务Transactional和动态代理(三)-事务失效的场景
    Spring事务Transactional和动态代理(二)-cglib动态代理
    Spring事务Transactional和动态代理(一)-JDK代理实现
    深入JVM(二)JVM概述
    深入JVM(一)JVM指令手册
    Spark RDD的弹性到底指什么
    JDK源码分析-HashMap
    Spark性能优化指南——初级篇
    mysql 复制表结构
  • 原文地址:https://www.cnblogs.com/renzm0318/p/11171693.html
Copyright © 2020-2023  润新知