vue slot 插槽

https://cn.vuejs.org/v2/guide/components.html

https://blog.csdn.net/sinat_17775997/article/details/52484072

通过插槽分发内容

和 HTML 元素一样,我们经常需要向一个组件传递内容,像这样:

<alert-box>
  Something bad happened.
</alert-box>

可能会渲染出这样的东西:

Error! Something bad happened.

幸好,Vue 自定义的 <slot> 元素让这变得非常简单:

Vue.component('alert-box', {
  template: `
    <div class="demo-alert-box">
      <strong>Error!</strong>
      <slot></slot>
    </div>
  `
})

如你所见,我们只要在需要的地方加入插槽就行了——就这么简单!

到目前为止,关于插槽你需要了解的大概就这些了,如果你阅读完本页内容并掌握了它的内容,我们会推荐你再回来把插槽读完。



(二十八)Slot分发内容

①概述:

简单来说,假如父组件需要在子组件内放一些DOM,那么这些DOM是显示、不显示、在哪个地方显示、如何显示,就是slot分发负责的活。

 

②默认情况下

父组件在子组件内套的内容,是不显示的。

例如代码:

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <div id="app">  
  2.     <children>  
  3.         <span>12345</span>  
  4.         <!--上面这行不会显示-->  
  5.     </children>  
  6. </div>  
  7. <script>  
  8.     var vm = new Vue({  
  9.         el: '#app',  
  10.         components: {  
  11.             children: {    //这个无返回值,不会继续派发  
  12.                 template: "<button>为了明确作用范围,所以使用button标签</button>"  
  13.             }  
  14.         }  
  15.     });  
  16. </script>  

显示内容是一个button按钮,不包含span标签里面的内容;

 

 

③单个slot

简单来说,只使用这个标签的话,可以将父组件放在子组件的内容,放到想让他显示的地方。

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <div id="app">  
  2.     <children>  
  3.         <span>12345</span>  
  4.         <!--上面这行不会显示-->  
  5.     </children>  
  6. </div>  
  7. <script>  
  8.     var vm = new Vue({  
  9.         el: '#app',  
  10.         components: {  
  11.             children: {    //这个无返回值,不会继续派发  
  12.                 template: "<button><slot></slot>为了明确作用范围,所以使用button标签</button>"  
  13.             }  
  14.         }  
  15.     });  
  16. </script>  

例如这样写的话,结果是:

<button><span>12345</span>为了明确作用范围,所以使用button标签</button>

 

即父组件放在子组件里的内容,插到了子组件的<slot></slot>位置;

注意,即使有多个标签,会一起被插入,相当于用父组件放在子组件里的标签,替换了<slot></slot>这个标签。

 

 

④具名slot

将放在子组件里的不同html标签放在不同的位置

父组件在要分发的标签里添加 slot=”name 属性

子组件在对应分发的位置的slot标签里,添加name=”name” 属性,

然后就会将对应的标签放在对应的位置了。

 

示例代码:

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <div id="app">  
  2.     <children>  
  3.         <span slot="first">12345</span>  
  4.         <span slot="second">56789</span>  
  5.         <!--上面这行不会显示-->  
  6.     </children>  
  7. </div>  
  8. <script>  
  9.     var vm = new Vue({  
  10.         el: '#app',  
  11.         components: {  
  12.             children: {    //这个无返回值,不会继续派发  
  13.                 template: "<button><slot name='first'></slot>为了明确作用范围,<slot name='second'></slot>所以使用button标签</button>"  
  14.             }  
  15.         }  
  16.     });  
  17. </script>  

显示结果为:(为了方便查看,已手动调整换行)

<button>

<span slot="first">12345</span>

为了明确作用范围,

<span slot="second">56789</span>

所以使用button标签

</button>


⑤分发内容的作用域:

被分发的内容的作用域,根据其所在模板决定,例如,以上标签,其在父组件的模板中(虽然其被子组件的children标签所包括,但由于他不在子组件的template属性中,因此不属于子组件),则受父组件所控制。

 

示例代码:

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <div id="app">  
  2.     <children>  
  3.         <span slot="first" @click="tobeknow">12345</span>  
  4.         <span slot="second">56789</span>  
  5.         <!--上面这行不会显示-->  
  6.     </children>  
  7. </div>  
  8. <script>  
  9.     var vm = new Vue({  
  10.         el: '#app',  
  11.         methods: {  
  12.             tobeknow: function () {  
  13.                 console.log("It is the parent's method");  
  14.             }  
  15.         },  
  16.         components: {  
  17.             children: {    //这个无返回值,不会继续派发  
  18.                 template: "<button><slot name='first'></slot>为了明确作用范围,<slot name='second'></slot>所以使用button标签</button>"  
  19.             }  
  20.         }  
  21.     });  
  22. </script>  

当点击文字12345的区域时(而不是按钮全部),会触发父组件的tobeknow方法。

 

但是点击其他区域时则没有影响

 

官方教程是这么说的:

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

 

 

 

⑥当没有分发内容时的提示:

假如父组件没有在子组件中放置有标签,或者是父组件在子组件中放置标签,但有slot属性,而子组件中没有该slot属性的标签。

 

那么,子组件的slot标签,将不会起到任何作用。

 

除非,该slot标签内有内容,那么在无分发内容的时候,会显示该slot标签内的内容。

 

如示例代码:

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <div id="app">  
  2.     <children>  
  3.         <span slot="first">【12345】</span>  
  4.         <!--上面这行不会显示-->  
  5.     </children>  
  6. </div>  
  7. <script>  
  8.     var vm = new Vue({  
  9.         el: '#app',  
  10.         components: {  
  11.             children: {    //这个无返回值,不会继续派发  
  12.                 template: "<div><slot name='first'><button>【如果没有内容则显示我1】</button></slot>为了明确作用范围,<slot name='last'><button>【如果没有内容则显示我2】</button></slot>所以使用button标签</div> 
  13.             }  
  14.         }  
  15.     });  
  16. </script>  

说明:

【1】name=’first’的slot标签被父组件对应的标签所替换(slot标签内部的内容被舍弃);

【2】name=’last’的slot标签,因为没有对应的内容,则显示该slot标签内部的内容

 

 

 

⑦假如想控制子组件根标签的属性

【1】首先,由于模板标签是属于父组件的,因此,将子组件的指令绑定在模板标签里,是不可以的(因为他归属于父组件);

 

【2】假如需要通过父组件控制子组件是否显示(例如v-if或者v-show),那么这个指令显然是属于父组件的(例如放在父组件的data下面)。可以将标签写在子组件的模板上。

如代码:

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <div id="app">  
  2.     <button @click="toshow">点击让子组件显示</button>  
  3.     <children v-if="abc">  
  4.     </children>  
  5. </div>  
  6. <script>  
  7.     var vm = new Vue({  
  8.         el: '#app',  
  9.         data: {  
  10.             abc: false  
  11.         },  
  12.         methods: {  
  13.             toshow: function () {  
  14.                 this.abc = !this.abc;  
  15.             }  
  16.         },  
  17.         components: {  
  18.             children: {    //这个无返回值,不会继续派发  
  19.                 template: "<div>这里是子组件</div>"  
  20.             }  
  21.         }  
  22.     });  
  23. </script>  


说明:

通过父组件(点击按钮,切换v-if指令的值)控制子组件是否显示。

 

 

【3】假如需要通过子组件,控制子组件是否显示(比如让他隐藏),那么这个指令显然是属于子组件的(会将值放在子组件的data属性下),那么就不能像上面这么写,而是必须放置在子组件的根标签中。

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <div id="app">  
  2.     <button @click="toshow">点击让子组件显示</button>  
  3.     <children>  
  4.         <span slot="first">【12345】</span>  
  5.         <!--上面这行不会显示-->  
  6.     </children>  
  7. </div>  
  8. <script>  
  9.     var vm = new Vue({  
  10.         el: '#app',  
  11.         methods: {  
  12.             toshow: function () {  
  13.                 this.$children[0].tohidden = true;  
  14.             }  
  15.         },  
  16.         components: {  
  17.             children: {    //这个无返回值,不会继续派发  
  18.                 template: "<div v-if='tohidden' @click='tohide'>这里是子组件</div>",  
  19.                 data: function () {  
  20.                     return {  
  21.                         tohidden: true  
  22.                     }  
  23.                 },  
  24.                 methods: {  
  25.                     tohide: function () {  
  26.                         this.tohidden = !this.tohidden;  
  27.                     }  
  28.                 }  
  29.             }  
  30.         }  
  31.     });  
  32. </script>  

说明:

点击子组件会让子组件消失;

点击父组件的按钮,通过更改子组件的tohidden属性,让子组件重新显示。

子组件的指令绑定在子组件的模板之中(如此才能调用);


猜你喜欢

转载自blog.csdn.net/zgpeterliu/article/details/80366902