zoukankan      html  css  js  c++  java
  • vue 插槽

    默认插槽(匿名插槽)

    father.vue

    <template>
      <div class="father">
        <h3>这里是父组件</h3>
        <child>
          <div class="tmpl">
            <span>菜单1</span>
            <span>菜单2</span>
          </div>
        </child>
      </div>
    </template>
    
    <script>
      import child from "./child";
    
      export default {
        name: "father",
        components: {
          child
        }
      }
    </script>

     因为父组件在里面写了html模板,那么子组件的匿名插槽这块模板就是下面这样。也就是说,子组件的匿名插槽被使用了,是被下面这块模板使用了。

    child.vue

    <template>
      <div class="child">
        <h3>这里是子组件</h3>
        <slot></slot>
      </div>
    </template>
    
    <script>
      export default {
        name: "slot"
      }
    </script>

    具名插槽

    father.vue

    <template>
      <div class="father">
        <h3>这里是父组件</h3>
        <child>
          <div class="tmpl" slot="up">
            <span>菜单1</span>
            <span>菜单2</span>
          </div>
          <div class="tmpl" slot="down">
            <span>菜单-1</span>
            <span>菜单-2</span>
          </div>
          <div class="tmpl">
            <span>菜单->1</span>
            <span>菜单->2</span>
          </div>
        </child>
      </div>
    </template>
    
    <script>
      import child from "./child";
    
      export default {
        name: "father",
        components: {
          child
        }
      }
    </script>

    child.vue

    <template>
      <div class="child">
        <h3>这里是子组件</h3>
        <!--    具名插槽-->
        <slot name="up"></slot>
        <!--    具名插槽-->
        <slot name="down"></slot>
        <!--    匿名插槽-->
        <slot></slot>
      </div>
    </template>
    
    <script>
      export default {
        name: "slots"
      }
    </script>

    作用域插槽(带数据的插槽)

    father.vue

    <template>
      <div class="father">
        <h3>这里是父组件</h3>
        <child>
          <div class="tmpl" slot="up">
            <span>具名插槽-up-1</span>
            <span>具名插槽-up-2</span>
          </div>
        </child>
        <child>
          <div class="tmpl" slot="down">
            <span>具名插槽-down-1</span>
            <span>具名插槽-down-2</span>
          </div>
        </child>
        <child>
          <div class="tmpl">
            <span>匿名插槽-1</span>
            <span>匿名插槽-2</span>
          </div>
        </child>
        <child-data>
          <template slot-scope="props">
            <span>作用域插槽</span>
            {{props.data.name}}
          </template>
        </child-data>
      </div>
    </template>
    
    <script>
      import child from "./child";
      import childData from "./childData";
    
      export default {
        name: "father",
        components: {
          child,
          childData
        }
      }
    </script>

    child.vue 同上

    childData.vue

    <template>
      <div class="child">
        <!--    作用域插槽-->
        <slot :data="data"></slot>
      </div>
    </template>
    
    <script>
      export default {
        name: "childData",
        data: function () {
          return {
            data: {
              name: 'vue',
              isUse: true
            }
          }
        },
      }
    </script>
  • 相关阅读:
    flask ajax
    python 符合条件跳过下一次循环
    python使用openpyxl excel 合并拆分单元格
    等价类划分法
    python 同级目录包导入问题,使用"."错误
    django:查询,反向查询
    Python实现程序执行次数的计数
    python 2x SSH通道连接服务器读取数据库和中文编码问题
    Python for 循环中使用append()添加可变元素,前面的值被覆盖,循环中内存应用地址不变
    以概率列表选择对应元素,轮盘概率选择Python实现
  • 原文地址:https://www.cnblogs.com/ronle/p/12179743.html
Copyright © 2011-2022 走看看