插槽

作者: kouss | 来源:发表于2019-07-10 18:07 被阅读0次

插槽就是Vue实现的一套内容分发的API,将<slot></slot>元素作为承载分发内容的出口。

这句话的意思就是,没有插槽的情况下在组件标签内些一些内容是不起任何作用的,当我在组件中声明了slot元素后,在组件元素内写的内容就会跑到它这里了!

1.单个插槽 | 匿名插槽

1.1<navigation-link>子组件定义为:

<a v-bind:href="url" class="nav-link">

    <slot></slot>

</a>

1.2父组件像以下这样使用<navigation-link>子组件:

<navigation-link url="/profile"> 

     Your Profile

</navigation-link>

1.3渲染出来的 HTML 将会是:

<a v-bind:href="url" class="nav-link"> 

     Your Profile

</a>

2.具名插槽

需要多个插槽时,可以利用<slot>元素的一个特殊的特性:name来定义具名插槽

2.1<base-layout>子组件模板定义:

<div class="container">

  <header>

    <slot name="header"></slot>

  </header>

  <main>

    <slot></slot>

  </main>

  <footer>

    <slot name="footer"></slot>

  </footer>

</div>

2.2.1父组件使用子组件<base-layout>,节点上使用slot特性:

<base-layout>

  <h1 slot="header">Here might be a page title</h1>

  <p>A paragraph for the main content.</p>

  <p>And another one.</p>

  <p slot="footer">Here's some contact info</p>

</base-layout>

2.2.2也可在内容外层套一个节点,并在外层节点上使用slot特性:

<base-layout>

  <template slot="header">

    <h1>Here might be a page title</h1>

  </template>

  <p>A paragraph for the main content.</p>

  <p>And another one.</p>

  <template slot="footer">

    <p>Here's some contact info</p>

  </template>

</base-layout>

2.3渲染出来的 HTML 都将会是:

<div class="container">

  <header>

    <h1>Here might be a page title</h1>

  </header>

  <main>

    <p>A paragraph for the main content.</p>

    <p>And another one.</p>

  </main>

  <footer>

    <p>Here's some contact info</p>

  </footer>

</div>

3.作用域插槽——带数据的插槽

单个插槽和具名插槽中插槽上不绑定数据,所以父组件提供的模板既要包括样式又要包括数据,而作用域插槽是子组件提供数据,父组件只需要提供一套样式

3.1子组件:

<template>

  <div class="child">

    <h3>这里是子组件</h3>

    // 作用域插槽

    <slot  :data="data"></slot>

  </div>

</template>

export default {

    data: function(){

      return {

        data: ['zhangsan','lisi','wanwu','zhaoliu','tianqi','xiaoba']

      }

    }

}

3.2父组件:

<template>

  <div class="father">

    <h3>这里是父组件</h3>

    <!--第一次使用:用flex展示数据-->

    <child>

      <template slot-scope="user">

        <div class="tmpl">

          <span v-for="item in user.data">{{item}}</span>

        </div>

      </template>

    </child>

    <!--第二次使用:用列表展示数据-->

    <child>

      <template slot-scope="user">

        <ul>

          <li v-for="item in user.data">{{item}}</li>

        </ul>

      </template>

    </child>

    <!--第三次使用:直接显示数据-->

    <child>

      <template slot-scope="user">

      {{user.data}}

      </template>

    </child>

    <!--第四次使用:不使用其提供的数据, 作用域插槽退变成匿名插槽-->

    <child>

      我就是模板

    </child>

  </div>

</template>

3.3结果如图:

匿名插槽和具名插槽详情见:https://cn.vuejs.org/v2/guide/components-slots.html#作用域插槽

作用域插槽详情见:https://segmentfault.com/a/1190000013277423

相关文章

  • vue----slot插槽

    插槽分类 匿名插槽 具名插槽 作用域插槽

  • vue中slot插槽的使用

    插槽的种类:1、默认插槽、具名插槽、作用域插槽、解构插槽、动态插槽几种。转原文:https://www.jians...

  • vue3中的插槽

    插槽 默认插槽 具名插槽,v-slot可以简写为# 动态插槽 #[dynamicSlotName] 作用域插槽(...

  • 2.插槽

    匿名插槽 具名插槽 作用域插槽

  • 深入理解vue中的slot与slot-scope(自 2.6.0

    单个插槽 | 默认插槽 | 匿名插槽首先是单个插槽,单个插槽是vue的官方叫法,但是其实也可以叫它默认插槽,或者与...

  • vue 插槽 slot

    插槽使用 普通插槽 具名插槽 使用具名插槽 从插槽里面传值出来如何接收? 如: 如何判断某个插槽是否被使用 组件内...

  • vue插槽

    默认插槽(没有名字的插槽) 具名插槽(带名字的插槽) 老版 2.6.0以前 新版 作用域插槽(父组件可以获取子组件...

  • Vue总结4-插槽,Vuex,VueRouter

    1.插槽 1.1匿名插槽 52-Vue组件-匿名插槽 ...

  • 组件化高级

    插槽 什么是插槽 生活中有很多地方都有用到插槽,比如电脑的USB插槽,插板上的电源插槽,目的是让我们原来的设备具备...

  • vue 插槽slot

    插槽的定义: 插槽的使用:

网友评论

      本文标题:插槽

      本文链接:https://www.haomeiwen.com/subject/pukkkctx.html