<!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>
//插入多個solt
<!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>
轉:https://www.jianshu.com/p/559332a9c123