Vue <slot> 元素
示例
使用內建的 <slot>
元素將父元件的內容放置到子元件的 <template>
中。
<template>
<div>
<p>SlotComp.vue</p>
<slot></slot>
</div>
</template>
執行示例 »
更多示例請參見下方。
定義和用法
內建的 <slot>
元素用於放置從父元件接收的內容。
當呼叫子元件時,在開始標籤和結束標籤之間提供的內容將出現在子元件內 <slot>
元素所在的位置。
一個元件可以包含多個 <slot>
,並且可以使用 name
prop 為插槽命名。對於包含不同命名插槽的元件,我們可以使用 v-slot
指令將內容傳送到特定的插槽。(示例 3)
如果父級未提供內容,<slot>
元素的開始標籤和結束標籤之間的內容將用作備用內容。(示例 5)
可以透過 <slot>
props 將資訊提供給父元素。(示例 8)
<slot>
元素只是內容的佔位符,<slot>
元素本身不會渲染成 DOM 元素。
Props
Prop | 描述 | |
---|---|---|
[any] | 在插槽中定義的 props 會建立“作用域插槽”,這些 props 會發送給父級。 | 執行示例 » |
name | 為插槽命名,以便父級可以使用 v-slot 指令將內容定向到特定的插槽。 |
執行示例 » |
更多示例
示例 1
使用插槽來包裹更大塊的動態 HTML 內容,以獲得卡片式的外觀。
App.vue
:
<template>
<h3>Slots in Vue</h3>
<p>We create card-like div boxes from the foods array.</p>
<div id="wrapper">
<slot-comp v-for="x in foods">
<img v-bind:src="x.url">
<h4>{{x.name}}</h4>
<p>{{x.desc}}</p>
</slot-comp>
</div>
</template>
當內容進入包含 <slot>
的元件時,我們使用一個 div 包裹 <slot>
,並對 <div>
進行區域性樣式設定,以在內容周圍建立卡片式外觀,而不會影響應用程式中的其他 div。
SlotComp.vue
:
<template>
<div> <!-- This div makes the card-like appearance -->
<slot></slot>
</div>
</template>
<script></script>
<style scoped>
div {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
border-radius: 10px;
margin: 10px;
}
</style>
執行示例 »
示例 2
使用插槽建立頁尾。
App.vue
:
<template>
<h3>Reusable Slot Cards</h3>
<p>We create card-like div boxes from the foods array.</p>
<p>We also create a card-like footer by reusing the same component.</p>
<div id="wrapper">
<slot-comp v-for="x in foods">
<img v-bind:src="x.url">
<h4>{{x.name}}</h4>
</slot-comp>
</div>
<footer>
<slot-comp>
<h4>Footer</h4>
</slot-comp>
</footer>
</template>
執行示例 »
示例 3
使用插槽名稱,內容可以傳送到特定的插槽。
SlotComp.vue
:
<h3>Component</h3>
<div>
<slot name="topSlot"></slot>
</div>
<div>
<slot name="bottomSlot"></slot>
</div>
App.vue
:
<h1>App.vue</h1>
<p>The component has two div tags with one slot in each.</p>
<slot-comp v-slot:bottomSlot>'Hello!'</slot-comp>
執行示例 »
示例 4
在一個元件中有兩個插槽時,傳送到該元件的內容將出現在兩個插槽中。
App.vue
:
<h1>App.vue</h1>
<p>The component has two div tags with one slot in each.</p>
<slot-comp>'Hello!'</slot-comp>
SlotComp.vue
:
<h3>Component</h3>
<div>
<slot></slot>
</div>
<div>
<slot></slot>
</div>
執行示例 »
示例 5
在插槽中使用備用內容,以便在父級未提供內容時渲染一些內容。
App.vue
:
<template>
<h3>Slots Fallback Content</h3>
<p>A component without content provided can have fallback content in the slot tag.</p>
<slot-comp>
<!-- Empty -->
</slot-comp>
<slot-comp>
<h4>This content is provided from App.vue</h4>
</slot-comp>
</template>
SlotComp.vue
:
<template>
<div>
<slot>
<h4>This is fallback content</h4>
</slot>
</div>
</template>
執行示例 »
示例 6
沒有名稱的插槽將成為父級內容的預設插槽。
SlotComp.vue
:
<h3>Component</h3>
<div>
<slot></slot>
</div>
<div>
<slot name="bottomSlot"></slot>
</div>
App.vue
:
<h1>App.vue</h1>
<p>The component has two div tags with one slot in each.</p>
<slot-comp>'Hello!'</slot-comp>
執行示例 »
示例 7
使用 v-slot:default
明確定義一個插槽為預設插槽。
SlotComp.vue
:
<h3>Component</h3>
<div>
<slot></slot>
</div>
<div>
<slot name="bottomSlot"></slot>
</div>
App.vue
:
<h1>App.vue</h1>
<p>The component has two div tags with one slot in each.</p>
<slot-comp v-slot:default>'Default slot'</slot-comp>
執行示例 »
示例 8
作用域插槽:在插槽中使用 'foodName' prop 將食物名稱傳遞給父級。
SlotComp.vue
:
<template>
<slot
v-for="x in foods"
:key="x"
:foodName="x"
></slot>
</template>
<script>
export default {
data() {
return {
foods: ['Apple','Pizza','Rice','Fish','Cake']
}
}
}
</script>
App.vue
:
<slot-comp v-slot="food">
<h2>{{ food.foodName }}</h2>
</slot-comp>
執行示例 »
示例 9
作用域插槽:在插槽中使用 props 將基於物件陣列的多個資訊傳遞給父級。
SlotComp.vue
:
<template>
<slot
v-for="x in foods"
:key="x.name"
:foodName="x.name"
:foodDesc="x.desc"
:foodUrl="x.url"
></slot>
</template>
<script>
export default {
data() {
return {
foods: [
{ name: 'Apple', desc: 'Apples are a type of fruit that grow on trees.', url: 'img_apple.svg' },
{ name: 'Pizza', desc: 'Pizza has a bread base with tomato sauce, cheese, and toppings on top.', url: 'img_pizza.svg' },
{ name: 'Rice', desc: 'Rice is a type of grain that people like to eat.', url: 'img_rice.svg' },
{ name: 'Fish', desc: 'Fish is an animal that lives in water.', url: 'img_fish.svg' },
{ name: 'Cake', desc: 'Cake is something sweet that tastes good but is not considered healthy.', url: 'img_cake.svg' }
]
}
}
}
</script>
App.vue
:
<slot-comp v-slot="food">
<hr>
<h2>{{ food.foodName }}<img :src=food.foodUrl></h2>
<p>{{ food.foodDesc }}</p>
</slot-comp>
執行示例 »
示例 10
使用命名作用域插槽將一個文字放入 "leftSlot",另一個文字放入 "rightSlot"。
SlotComp.vue
:
<template>
<slot
name="leftSlot"
:text="leftText"
></slot>
<slot
name="rightSlot"
:text="rightText"
></slot>
</template>
<script>
export default {
data() {
return {
leftText: 'This text belongs to the LEFT slot.',
rightText: 'This text belongs to the RIGHT slot.'
}
}
}
</script>
App.vue
:
<slot-comp #leftSlot="leftProps">
<div>{{ leftProps.text }}</div>
</slot-comp>
<slot-comp #rightSlot="rightProps">
<div>{{ rightProps.text }}</div>
</slot-comp>
執行示例 »
相關頁面
Vue 教程:Vue 插槽
Vue 教程:Vue v-slot
Vue 教程:作用域插槽
Vue 教程:元件
Vue 參考:Vue v-slot 指令
Vue 參考:Vue $slots 物件