這篇文章將為大家詳細(xì)講解有關(guān)vue組件化中slot怎么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司專注于額敏網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供額敏營銷型網(wǎng)站建設(shè),額敏網(wǎng)站制作、額敏網(wǎng)頁設(shè)計、額敏網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造額敏網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供額敏網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
前言
slot可以在子組件中開啟插槽,在父組件引用該組建時,可以定義這個插槽內(nèi)要展現(xiàn)的功能或模塊。
1.單個slot
子組件中在相應(yīng)位置寫slot標(biāo)簽,父組件在引用子組件時,在子組件標(biāo)簽內(nèi)寫要插入插槽的元素;
還可以設(shè)置slot在父組件沒有設(shè)置插槽時,子組件的插槽默認(rèn)顯示內(nèi)容;
父組件.vue
<template>
<div class="home">
<child-componment>
<p>
這是父組件的slot替代內(nèi)容!
</p>
</child-componment>
</div>
</template>
<script>
import childComponment from '@/components/childComponment.vue'
export default {
name: "home",
components:{
childComponment
},
data(){
return {
message: ''
}
}
};
</script>子組件childComponment.vue
<template>
<div class="childComponment">
<h3>這是子組件childComponment!</h3>
<slot>
<span >如果父組件沒有插入內(nèi)容,我這樣可以設(shè)置默認(rèn)的顯示內(nèi)容</span>
</slot>
</div>
</template>
<script>
export default {
name: "childComponment",
data(){
return {
message: ''
}
}
};
</script>2.具名slot(同時使用多個插槽)
給slot指定一個名稱,可以分發(fā)多個slot插槽,但是只能有一個無名slot;
父組件的slot插槽內(nèi)容,不寫slot="xxx"的都會插到子組件的無名slot中;
如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會被拋棄。
<template>
<div class="home">
<child-componment>
<h2 slot="header">
父組件的header
</h2>
<h7 slot="footer">父組件的footer</h7>
<h7>父組件的無名slot-1</h7>
<p>
父組件的無名slot-2
</p>
</child-componment>
</div>
</template>
<script>
import childComponment from '@/components/childComponment.vue'
export default {
name: "home",
components:{
childComponment
},
data(){
return {
message: ''
}
}
};
</script>子組件
<template>
<div class="childComponment">
<span>這是子組件childComponment的正常內(nèi)容!</span>
<div class="header">
<slot name="header">
<span >子組件默認(rèn)header-slot</span>
</slot>
</div>
<div class="container">
<!-- 如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會被拋棄 -->
<slot>
<span >子組件默認(rèn)無名slot</span>
</slot>
</div>
<div class="footer">
<slot name="footer">
<span >子組件默認(rèn)footer-slot</span>
</slot>
</div>
</div>
</template>
<script>
export default {
name: "childComponment",
data(){
return {
message: ''
}
}
};
</script>
<style scoped>
.childComponment{
font-size: 16px;
}
.header{
height: 100px;
border:1px solid red;
color: red;
}
.container{
height: 500px;
border: 1px solid black;
color: black;
}
.footer{
height:100px;
border: 1px grey solid;
color: grey;
}
</style>
3.作用域插槽
<template>
<div class="home">
<child-componment>
<template slot-scope="slotProps">
<!-- 這里顯示子組件傳來的數(shù)據(jù) -->
<p>{{slotProps}}</p>
</template>
</child-componment>
</div>
</template>
<script>
import childComponment from '@/components/childComponment.vue'
export default {
name: "home",
components:{
childComponment
}
};
</script>子組件
<template> <div class="childComponment"> <span>這是子組件childComponment的正常內(nèi)容!</span> <div class="container"> <!-- 如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會被拋棄 --> <slot msg="子組件信息" slotData="子組件數(shù)據(jù)"></slot> </div> </div> </template>
Tips:
作用于插槽也可是具名插槽
案例:列表組件
這是作用于插槽使用最多的案例,允許組件自定義應(yīng)該如何渲染組件的每一項。
<template>
<div class="about">
<h2>This is about page</h2>
<my-list :books="books">
<template slot="bookList" slot-scope="myListProps">
<li>{{myListProps.bookName}}</li>
</template>
</my-list>
</div>
</template>
<script>
import myList from '@/components/myList.vue'
export default {
components:{
myList
},
data(){
return {
books: [
{name: 'css揭秘'},
{name: '深入淺出nodejs'},
{name: 'javascript設(shè)計模式與開發(fā)實戰(zhàn)'}
]
}
}
}
</script>子組件myList.vue
<template>
<div class="myList">
<h2>This is myList page</h2>
<ul>
<slot name="bookList" v-for="book in books" :bookName="book.name"></slot>
</ul>
</div>
</template>
<script>
export default {
props:{
books:{
type: Array,
default: function(){
return []
}
}
},
mounted(){
console.log(this.books)
}
}
</script>其實上面的案例可直接在父組件中for循環(huán)就好,此處只是作為演示slot的作用域插槽;
實際開發(fā)中作用域插槽的使用場景主要為:既可以復(fù)用子組件的slot,又可以使slot內(nèi)容不一致。
4.訪問slot
vue2.0提供了$slot方法來訪問slot
此處代碼以**“具名slot(同時使用多個插槽)”**的代碼為例,修改一下子組件childComponment.vue
export default {
name: "childComponment",
data(){
return {
message: ''
}
},
mounted(){
let header = this.$slots.header
let main = this.$slots.default
let footer = this.$slots.footer
console.log(header)
console.log(main)
console.log(footer)
console.log(footer[0].elm.innerHTML)
}
};打印結(jié)果:

其中elm的內(nèi)容為插槽內(nèi)容,結(jié)構(gòu)如下:

關(guān)于“vue組件化中slot怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
新聞名稱:vue組件化中slot怎么用
分享網(wǎng)址:http://chinadenli.net/article18/ppsodp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站策劃、App設(shè)計、網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)