美文网首页Vue前端程序员
Vue学习笔记入门篇——组件的内容分发(slot)

Vue学习笔记入门篇——组件的内容分发(slot)

作者: ChainZhang | 来源:发表于2017-07-10 23:25 被阅读1077次

本文为转载,原文:Vue学习笔记入门篇——组件的内容分发(slot)

介绍

为了让组件可以组合,我们需要一种方式来混合父组件的内容与子组件自己的模板。这个过程被称为 内容分发 (或 “transclusion” 如果你熟悉 Angular)。Vue.js 实现了一个内容分发 API,使用特殊的 'slot' 元素作为原始内容的插槽。

编译作用域

在深入内容分发 API 之前,我们先明确内容在哪个作用域里编译。假定模板为:

<child-component>
  {{ message }}
</child-component>

message 应该绑定到父组件的数据,还是绑定到子组件的数据?答案是父组件。组件作用域简单地说是:

父组件模板的内容在父组件作用域内编译;
子组件模板的内容在子组件作用域内编译。

一个常见错误是试图在父组件模板内将一个指令绑定到子组件的属性/方法:

<!-- 无效 -->
<child-component v-show="someChildProperty"></child-component>

假定 someChildProperty 是子组件的属性,上例不会如预期那样工作。父组件模板不应该知道子组件的状态。
如果要绑定作用域内的指令到一个组件的根节点,你应当在组件自己的模板上做:

Vue.component('child-component', {
  // 有效,因为是在正确的作用域内
  template: '<div v-show="someChildProperty">Child</div>',
  data: function () {
    return {
      someChildProperty: true
    }
  }
})

类似地,分发内容是在父作用域内编译。

单个slot

除非子组件模板包含至少一个 'slot' 插口,否则父组件的内容将会被丢弃。当子组件模板只有一个没有属性的 slot 时,父组件整个内容片段将插入到 slot 所在的 DOM 位置,并替换掉 slot 标签本身。
最初在 'slot' 标签中的任何内容都被视为备用内容。备用内容在子组件的作用域内编译,并且只有在宿主元素为空,且没有要插入的内容时才显示备用内容。
示例代码:

<div id="app">
    <h1>我是父组件的标题</h1>
    <my-component>
        <p>初始内容1</p>
        <p>初始内容2</p>
    </my-component>
</div>
Vue.component('my-component',{
    template:`
    <div>
        <h2>我是子组件的标题</h2>
        <slot>
            只有在没有要分发的内容是才出现。
        </slot>
    <div>
`,
})
new Vue({
    el:'#app'
})

运行结果如下:



将html部分代码修改为以下代码:

<div id="app">
    <h1>我是父组件的标题</h1>
    <my-component>
    </my-component>
</div>

则运行结果如下:


具名slot

'slot' 元素可以用一个特殊的属性 name 来配置如何分发内容。多个 slot 可以有不同的名字。具名 slot 将匹配内容片段中有对应 slot 特性的元素。
仍然可以有一个匿名 slot,它是默认 slot,作为找不到匹配的内容片段的备用插槽。如果没有默认的 slot,这些找不到匹配的内容片段将被抛弃。
如以下例子:

<div id="app">
    <my-component>
        <h1 slot="header">这是标题</h1>
        <p>第一个段落</p>
        <p>第二个段落</p>
        <p>第三个段落</p>
        <p slot="footer">联系信息</p>
    </my-component>
</div>
Vue.component('my-component',{
    template:`
    <div class="container">
        <header>
            <slot name="header"></slot>
        </header>
        <main>
            <slot></slot>
        </main>
        <footer>
            <slot name="footer"></slot>
        </footer>
    <div>
`,
})
new Vue({
    el:'#app'
})

运行结果如下:



在组合组件时,内容分发 API 是非常有用的机制。

作用域插槽

2.1.0新增

作用域插槽是一种特殊类型的插槽,用作使用一个 (能够传递数据到) 可重用模板替换已渲染元素。
在子组件中,只需将数据传递到插槽,就像你将 props 传递给组件一样。
示例代码:

<div id="app">
    <my-component>
        <template scope="props">
            <p>hello from parent</p>
            <p>{{props.text}}</p>
        </template>
    </my-component>
</div>
Vue.component('my-component',{
    template:`
    <div class="child">
        <slot text="hello from child"></slot>
    <div>
`,
    props:['text']
})
new Vue({
    el:'#app'
})

运行结果:



在父级中,具有特殊属性 scope 的 <'template'> 元素必须存在,表示它是作用域插槽的模板。scope 的值对应一个临时变量名,此变量接收从子组件中传递的 props 对象。

作用域插槽更具代表性的用例是列表组件,允许组件自定义应该如何渲染列表每一项:

<div id="app">
    <my-component :items="items">
        <template slot="item" scope="props">
            <li>{{props.text}}</li>
        </template>
    </my-component>
</div>
Vue.component('my-component',{
    template:`
    <ul>
        <slot name="item" v-for="item in items" :text="item.text"></slot>
    </ul>
`,
    props:['text','items']
})
new Vue({
    el:'#app',
    data:{
        items:[
            {text:'item1'},
            {text:'item2'},
            {text:'item3'},
        ]
    }
})

作用域插槽也可以是具名的
运行结果:


本文为原创,转载请注明出处。
上一节:Vue学习笔记入门篇——组件的通讯
返回目录
下一节:Vue学习笔记入门篇——组件杂项

相关文章

  • Vue学习笔记入门篇——组件的内容分发(slot)

    本文为转载,原文:Vue学习笔记入门篇——组件的内容分发(slot) 介绍 为了让组件可以组合,我们需要一种方式来...

  • slot是什么?有什么作用?原理是什么?

    slot又名插槽,是Vue的内容分发机制,组件内部的模板引擎使用slot元素作为承载分发内容的出口。插槽slot是...

  • slot(插槽)

    slot又称插槽,是Vue的内容分发机制,组件内部的模板引擎使用slot元素作为承载分发内容的出口。插槽slot是...

  • Vue组件三-Slot分发内容

    Vue组件三-Slot分发内容 开始 Vue组件是学习Vue框架最比较难的部分,而这部分难点我认为可以分为三个部分...

  • 2020-07-23 一次性讲明白vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • Vue组件内容分发(slot)

    内容分发 为了让组件可以组合,我们需要一种方式来混合父组件的内容与子组件自己的模板。这个过程称为内容分发。 插槽 ...

  • 27.Vue slot内容分发-解构

    Vue slot的使用参考:26.Vue slot内容分发 什么是slot分发解构:官方定义:如果一个 JavaS...

  • 《Vue.js实战》学习笔记 slot

    Vue.js实战 props 传递数据,events 触发事件和 slot 内容分发构成了 Vue 组件的3个AP...

  • 日常工作知识点集合之vue(二)

    1.vue中element-ui实现的slot分发和route路由 slot分发其实就是父组件需要在子组件内放一些...

网友评论

本文标题:Vue学习笔记入门篇——组件的内容分发(slot)

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