zoukankan      html  css  js  c++  java
  • Vue学习笔记入门篇——组件的内容分发(slot)

    本文为转载,原文: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学习笔记入门篇——组件杂项

  • 相关阅读:
    C++结构体中sizeof
    sizeof()的用法
    XStream和Json
    省市联动
    ajax
    配置文件的读取
    JSP标签库
    字符串函数参数传入传出(去空格)
    字符串函数参数传入传出(字符串反转)
    opendir,readdir,closedir
  • 原文地址:https://www.cnblogs.com/ChainZhang/p/7148128.html
Copyright © 2011-2022 走看看