Vue:slot用法

单个 Slot
在子组件内使用特殊的<slot>元素就可以为这个子组件添加一个 slot (插槽),在父组件模板里,插入在子组件标签内的所有内容将替代子组件的<slot>标签及它的内容.示例代码如下:


<!DOCTYPE html>
<html> <head> <meta charset="utf-8"> <title>示例</title> </head> <body> <div id="app"> <child-component> <p>分发的内容</p> <p>更多分发的内容</p> </child-component> </div> <script src="https://unpkg.com/vue/dist/vue.min.js"></script> <script> Vue.component('child-component', { template: '\ <div>\ <slot>\ <p>如果父组件没用插入内容,我将作为默认出现</p>\ </slot>\ </div>' }); var app = new Vue({ el: '#app' }) </script> </body> </html> 

子组件 child-component 的模板内定义一个 <slot> 元素,并且用一个 <p> 作为默认的内容,在父组件没有使用 slot 时,会渲染这段默认的文本;如果写入了 slot ,那就会替换整个 <slot>.所以上列渲染后的结果为:

<div id="app"> <div> <p>分发的内容</p> <p>更多分发的内容</p> </div> </div> 

注意:子组件<slot>内的备用内容,它的作用域时子组件本身.
具名 Slot
给 <slot> 元素指定一个 name 后可以分发多个内容,具名 Slot 可以与单个 Slot 共存,例如下面的示例:


<!DOCTYPE html>
<html> <head> <meta charset="utf-8"> <title>示例</title> </head> <body> <div id="app"> <child-component> <h2 slot="header">标题</h2> <p>正文内容</p> <p>更多正文内容</p> <div slot="footer">底部信息</div> </child-component> </div> <script src="https://unpkg.com/vue/dist/vue.min.js"></script> <script> Vue.component('child-component', { template: '\ <div class="component">\ <div class="header">\ <slot name="header"></slot>\ </div>\ <div class="main">\ <slot></slot>\ </div>\ <div class="footer">\ <slot name="footer"></slot>\ </div>\ </div>' }); var app = new Vue({ el: '#app' }) </script> </body> </html> 

子组件内声明了3个 <slot> 元素,其中在<div class="main">内的<slot> 没用使用 name 特性,它将作为默认 slot 出现,父组件没有使用 slot 特性的元素与内容都将出现在这里.
如果没有指定默认的匿名 slot, 父组件内多余的内容片段都将被抛弃.
上例最终渲染后的结果为:

<div id="app"> <div class="container"> <div class="header"> <h2>标题</h2> </div> <div class="main"> <p>正文内容</p> <p>更多的正文内容</p> </div> <div class="footer"> <div>底部信息</div> </div> </div> </div> 

在组合使用组件时,内容分发API至关重要.



作者:一叶扁舟丶
链接:https://www.jianshu.com/p/559332a9c123
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

http://market.szonline.net/amaz/33831.html
http://market.szonline.net/amaz/33829.html
http://market.szonline.net/amaz/33827.html
http://market.szonline.net/amaz/33823.html
http://market.szonline.net/amaz/33818.html
http://market.szonline.net/amaz/33812.html
http://market.szonline.net/amaz/33807.html
http://market.szonline.net/amaz/33801.html
http://market.szonline.net/amaz/33796.html
http://market.szonline.net/amaz/33791.html
http://market.szonline.net/amaz/33786.html
http://market.szonline.net/amaz/33785.html
http://market.szonline.net/amaz/33782.html
http://market.szonline.net/amaz/33777.html
http://market.szonline.net/amaz/33771.html
http://market.szonline.net/amaz/33769.html
http://market.szonline.net/amaz/33767.html
http://market.szonline.net/amaz/33764.html
http://market.szonline.net/amaz/33763.html
http://market.szonline.net/amaz/33762.html
http://market.szonline.net/amaz/33761.html
http://market.szonline.net/amaz/33757.html
http://market.szonline.net/amaz/33751.html
http://market.szonline.net/amaz/33745.html
http://market.szonline.net/amaz/33740.html
http://market.szonline.net/amaz/33733.html
http://market.szonline.net/amaz/33728.html

猜你喜欢

转载自www.cnblogs.com/zjw2004112/p/12408639.html