源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-02-27 11:23 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解Vue学习笔记入门篇之组件的内容分发(slot)
[b]介绍[/b] 为了让组件可以组合,我们需要一种方式来混合父组件的内容与子组件自己的模板。这个过程被称为 内容分发 (或 “transclusion” 如果你熟悉 Angular)。Vue.js 实现了一个内容分发 API,使用特殊的 'slot' 元素作为原始内容的插槽。 [b]编译作用域[/b] 在深入内容分发 API 之前,我们先明确内容在哪个作用域里编译。假定模板为:
<child-component>
 {{ message }}
</child-component>
message 应该绑定到父组件的数据,还是绑定到子组件的数据?答案是父组件。组件作用域简单地说是: [list=1] [*]父组件模板的内容在父组件作用域内编译;[/*] [*]子组件模板的内容在子组件作用域内编译。[/*] [/list] 一个常见错误是试图在父组件模板内将一个指令绑定到子组件的属性/方法:
<!-- 无效 -->
<child-component v-show="someChildProperty"></child-component>
假定 someChildProperty 是子组件的属性,上例不会如预期那样工作。父组件模板不应该知道子组件的状态。 如果要绑定作用域内的指令到一个组件的根节点,你应当在组件自己的模板上做:
Vue.component('child-component', {
 // 有效,因为是在正确的作用域内
 template: '<div v-show="someChildProperty">Child</div>',
 data: function () {
  return {
   someChildProperty: true
  }
 }
})
类似地,分发内容是在父作用域内编译。 [b]单个slot[/b] 除非子组件模板包含至少一个 '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'
})
运行结果如下: [img]http://files.jb51.net/file_images/article/201707/2017071715384771.jpg[/img] 将html部分代码修改为以下代码:
<div id="app">
  <h1>我是父组件的标题</h1>
  <my-component>
  </my-component>
</div>
则运行结果如下: [img]http://files.jb51.net/file_images/article/201707/2017071715384772.jpg[/img] [b]具名slot[/b] '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'
})
运行结果如下: [img]http://files.jb51.net/file_images/article/201707/2017071715384873.jpg[/img] 在组合组件时,内容分发 API 是非常有用的机制。 [b]作用域插槽[/b] 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'
})
运行结果: [img]http://files.jb51.net/file_images/article/201707/2017071715384874.jpg[/img] 在父级中,具有特殊属性 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'},
    ]
  }
})
作用域插槽也可以是具名的 运行结果: [img]http://files.jb51.net/file_images/article/201707/2017071715384975.jpg[/img] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部