1.普通插槽
父組件
<template>
<van-panel title="普通插槽">
<Child>hello child,我來自父組件</Child>
</van-panel>
</template>
<script>
import Child from "./Child";
export default {
components: {
Child
}
};
</script>
<style>
</style>
子組件
<template>
<div style="border: 1px solid green;">
<h4>這是Child組件</h4>
<!-- 用來存放父組件的內(nèi)容 -->
<slot></slot>
<p>我喜歡編程</p>
</div>
</template>
<script>
export default {};
</script>
<style>
</style>
此時(shí)可以顯示父組件插在子組件里的內(nèi)容
2.具名插槽
父組件
<template>
<van-panel title="具名插槽">
<Child>
<p slot="left">
<van-button>左邊</van-button>
</p>
<p slot="right">
<van-button>右邊</van-button>
</p>
</Child>
<!-- vant的slot -->
<div slot="footer">
<van-button type="primary">點(diǎn)擊</van-button>
</div>
</van-panel>
</template>
<script>
import Child from "./Child";
export default {
components: {
Child
}
};
</script>
<style>
</style>
子組件
<template>
<div class="flex jc-sa" style="border: 1px solid green;">
<!-- 接收父組件對(duì)應(yīng)的內(nèi)容 -->
<slot name="left"></slot>
<!-- <slot name="right"></slot> -->
</div>
</template>
<script>
export default {};
</script>
<style>
</style>
接受了父組件傳來的slot 名字,并可以選擇使用
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。