標籤:位置 div 模板 包含 UI ejs logs html 基礎上
資料來自:https://cn.vuejs.org/v2/guide/components.html#具名-Slot
在官方文檔的基礎上,更加細緻的講解代碼。
<slot> 標籤中的任何內容都被視為備用內容,只有在宿主要元素為空白,它才顯示。
1.單個slot
除非子組件模板包含至少一個 <slot> 插口,否則父組件的內容將丟棄。
<body > <div id="app"> <h1>我是父組件的標題</h1> <my-component> <p>這是一些初始內容</p> <p>這是更多的初始內容</p> </my-component> </div> <script src="js/vue.min.js"></script> <script> new Vue({ el:‘#app‘, components:{ myComponent:{ template:‘<div><h2>我是子組件的標題</h2><slot>只有在沒有要分發的內容時才會顯示。</slot></div>‘, } } }) </script></body>2.具名slot
slot 可以有不同的名字。具名 slot 將匹配內容片段中所對應 slot 特性。
將對應的父組件嵌套到 具名 slot 的子組件 位置 的元素展示出來。
<app-layout> <h1 slot="header">這裡可能是一個頁面標題</h1> <p>主要內容的一個段落。</p> <p>另一個主要段落。</p> <p slot="footer">這裡有一些聯絡資訊</p> </app-layout> <script src="js/vue.min.js"></script> <script> new Vue({ el:‘#container‘, components:{ appLayout:{ template:‘<div class="container">
<header><slot name="header"></slot></header>
<main><slot></slot></main>
<footer><slot name="footer"></slot></footer>
</div>‘,
} } })
Vuejs-組件-<slot> 標籤分發內容