• vue插槽


    vue插槽:在引入的组件标签中书写的内容是不会显示的,如果要显示这部分内容,需要用到<slot></slot>

    1、插槽的概念:

    比如说在Father组件中引入了Child组件,

    Father:

    <template>
      <div>
        <Child url="http://www.baidu.com">
          <p>百度</p>
        </Child>
      </div>
    </template>

    Child:

    <template>
      <div>
        <a :href="url" target="_blank">
          <slot></slot>
        </a>
      </div>
    </template>
    <script>
      export default {
        props: ['url']
      }
    </script>

    如果不在Child中写slot标签,那么在Father组件中 <p>百度</p> 就不会有显示

    2、具名插槽和匿名插槽:

    Father:

    <template>
      <div>
        <Child>
          <div slot="header">
            <h1>标题</h1>
          </div>
          <div>
            <p>内容</p>
          </div>
          <div slot="footer">
            <p>底部</p>
          </div>
        </Child>
      </div>
    </template>

    Child:

    <template>
      <div>
        <header>
          <slot name="header"></slot>
        </header>
        <main>
          <slot></slot>
        </main>
        <footer>
          <slot name="footer"></slot>
        </footer>
      </div>
    </template>

    此时,在父组件中的具名插槽和匿名插槽会对应地显示在子组件相应的位置,注意2.6.0+版本后,父组件中的写法:

    <template>
      <div>
        <Child>
          <template v-slot:header>
            <h1>标题</h1>
          </template>
          <template>
            <p>内容</p>
          </template>
          <template v-slot:footer>
            <p>底部</p>
          </template>
        </Child>
      </div>
    </template>

    3、插槽作用域

    前面父传子使用的是props,那么子传父怎么办呢?

    ①先在子组件中用v-bind绑定一个属性值

    <template>
      <div>
        <a :href="url" target="_blank">
          <slot :val='"我是要传给父组件的值"'></slot>
        </a>
      </div>
    </template>

    ②在父组件中通过 v-slot='slotProps' 获取到属性集合对象

    <template>
      <div>
        <Child v-slot='slotProps'>
          <p>{{slotProps.val}}</p>
        </Child>
      </div>
    </template>

    也可以通过解构赋值的方式直接获取到当前属性

    <template>
      <div>
        <Child url='xxx' v-slot='{val}'>
          <p>{{val}}</p>
        </Child>
      </div>
    </template>

    4、具名插槽作用域

    Child:

    <template>
      <div>
        <header>
          <slot name="header" :val='111'></slot>
        </header>
        <main>
          <slot :val='100'></slot>
        </main>
        <footer>
          <slot name="footer"></slot>
        </footer>
      </div>
    </template>

    Father:

    <template>
      <div>
        <Child>
          <template v-slot:header={val}>
            <h1>标题</h1>
            <p>{{val}}</p>
          </template>
          <template v-slot='{val}'>
            <p>内容</p>
            <p>{{val}}</p>
          </template>
          <template v-slot:footer>
            <p>底部</p>
          </template>
        </Child>
      </div>
    </template>
  • 相关阅读:
    Centos7下PyCharm2019安装
    企业的web项目类型、企业项目开发流程、立项申请阶段、需求分析、导航菜单、轮播图、退出登录、登录注册、课程列表、创建虚拟环境、依赖包安装、创建项目、调整目录、创建代码版本git、日志配置、异常处理、创建数据库、
    增量式爬取阳光热线网
    分布式爬取阳光热线网
    什么是接口测试,为什么做接口测试
    什么是接口、接口优势、类型(详解)
    187. Repeated DNA Sequences
    274. H-Index
    299. Bulls and Cows
    36. Valid Sudoku
  • 原文地址:https://www.cnblogs.com/wuqilang/p/14811445.html
Copyright © 2020-2023  润新知