vue組件化中slot的基本使用方法
前言
slot可以在子組件中開啟插槽,在父組件引用該組建時(shí),可以定義這個(gè)插槽內(nèi)要展現(xiàn)的功能或模塊,下面話不多說了,來一起看看詳細(xì)的介紹吧
1.單個(gè)slot
子組件中在相應(yīng)位置寫slot標(biāo)簽,父組件在引用子組件時(shí),在子組件標(biāo)簽內(nèi)寫要插入插槽的元素;
還可以設(shè)置slot在父組件沒有設(shè)置插槽時(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">
<h2>這是子組件childComponment!</h2>
<slot>
<span style="color: red;">如果父組件沒有插入內(nèi)容,我這樣可以設(shè)置默認(rèn)的顯示內(nèi)容</span>
</slot>
</div>
</template>
<script>
export default {
name: "childComponment",
data(){
return {
message: ''
}
}
};
</script>
2.具名slot(同時(shí)使用多個(gè)插槽)
給slot指定一個(gè)名稱,可以分發(fā)多個(gè)slot插槽,但是只能有一個(gè)無名slot;
父組件的slot插槽內(nèi)容,不寫slot="xxx"的都會(huì)插到子組件的無名slot中;
如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會(huì)被拋棄。
<template>
<div class="home">
<child-componment>
<h1 slot="header">
父組件的header
</h1>
<h6 slot="footer">父組件的footer</h6>
<h6>父組件的無名slot-1</h6>
<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 style="color: red;">子組件默認(rèn)header-slot</span>
</slot>
</div>
<div class="container">
<!-- 如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會(huì)被拋棄 -->
<slot>
<span style="color: red;">子組件默認(rèn)無名slot</span>
</slot>
</div>
<div class="footer">
<slot name="footer">
<span style="color: red;">子組件默認(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)容將會(huì)被拋棄 --> <slot msg="子組件信息" slotData="子組件數(shù)據(jù)"></slot> </div> </div> </template>
Tips:
作用于插槽也可是具名插槽
案例:列表組件
這是作用于插槽使用最多的案例,允許組件自定義應(yīng)該如何渲染組件的每一項(xiàng)。
<template>
<div class="about">
<h1>This is about page</h1>
<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è)計(jì)模式與開發(fā)實(shí)戰(zhàn)'}
]
}
}
}
</script>
子組件myList.vue
<template>
<div class="myList">
<h1>This is myList page</h1>
<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>
其實(shí)上面的案例可直接在父組件中for循環(huán)就好,此處只是作為演示slot的作用域插槽;
實(shí)際開發(fā)中作用域插槽的使用場景主要為:既可以復(fù)用子組件的slot,又可以使slot內(nèi)容不一致。
4.訪問slot
vue2.0提供了$slot方法來訪問slot
此處代碼以**“具名slot(同時(shí)使用多個(gè)插槽)”**的代碼為例,修改一下子組件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)如下:

總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。
相關(guān)文章
el-form組件使用resetFields重置失效的問題解決
用el-form寫了包含三個(gè)字段的表單,使用resetFields方法進(jìn)行重置,發(fā)現(xiàn)點(diǎn)擊重置或要清空校驗(yàn)時(shí)是失效的,所以本文給大家介紹了el-form組件使用resetFields重置失效的問題解決,需要的朋友可以參考下2023-12-12
vue+iview+less 實(shí)現(xiàn)換膚功能
這篇文章主要介紹了vue+iview+less 實(shí)現(xiàn)換膚功能,項(xiàng)目搭建用的vue—cli,css框架選擇的iview,具體操作流程大家跟隨腳本之家小編一起看看吧2018-08-08
vue 路由緩存 路由嵌套 路由守衛(wèi) 監(jiān)聽物理返回操作
這篇文章主要介紹了vue 路由緩存 路由嵌套 路由守衛(wèi) 監(jiān)聽物理返回操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue左右側(cè)聯(lián)動(dòng)滾動(dòng)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue左右側(cè)聯(lián)動(dòng)滾動(dòng)的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

