slot能夠在子組件中開啓插槽,在父組件引用該組建時,能夠定義這個插槽內要展示的功能或模塊javascript
子組件中在相應位置寫slot標籤,父組件在引用子組件時,在子組件標籤內寫要插入插槽的元素; 還能夠設置slot在父組件沒有設置插槽時,子組件的插槽默認顯示內容; 父組件.vuecss
<template>
<div class="home">
<child-componment>
<p>
這是父組件的slot替代內容!
</p>
</child-componment>
</div>
</template>
<script>
import childComponment from '@/components/childComponment.vue'
export default {
name: "home",
components:{
childComponment
},
data(){
return {
message: ''
}
}
};
</script>
複製代碼
子組件childComponment.vuevue
<template>
<div class="childComponment">
<h2>這是子組件childComponment!</h2>
<slot>
<span style="color: red;">若是父組件沒有插入內容,我這樣能夠設置默認的顯示內容</span>
</slot>
</div>
</template>
<script>
export default {
name: "childComponment",
data(){
return {
message: ''
}
}
};
</script>
複製代碼
給slot指定一個名稱,能夠分發多個slot插槽,可是隻能有一個無名slot; 父組件的slot插槽內容,不寫slot="xxx"的都會插到子組件的無名slot中; 若是沒有指定無名slot(默認slot),父組件內多餘的內容將會被拋棄。java
<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>
複製代碼
子組件node
<template>
<div class="childComponment">
<span>這是子組件childComponment的正常內容!</span>
<div class="header">
<slot name="header">
<span style="color: red;">子組件默認header-slot</span>
</slot>
</div>
<div class="container">
<!-- 若是沒有指定無名slot(默認slot),父組件內多餘的內容將會被拋棄 -->
<slot>
<span style="color: red;">子組件默認無名slot</span>
</slot>
</div>
<div class="footer">
<slot name="footer">
<span style="color: red;">子組件默認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>
複製代碼
<template>
<div class="home">
<child-componment>
<template slot-scope="slotProps">
<!-- 這裏顯示子組件傳來的數據 -->
<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的正常內容!</span>
<div class="container">
<!-- 若是沒有指定無名slot(默認slot),父組件內多餘的內容將會被拋棄 -->
<slot msg="子組件信息" slotData="子組件數據"></slot>
</div>
</div>
</template>
複製代碼
Tips: 做用於插槽也但是具名插槽
這是做用於插槽使用最多的案例,容許組件自定義應該如何渲染組件的每一項。bash
<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設計模式與開發實戰'}
]
}
}
}
</script>
複製代碼
子組件myList.vueui
<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>
複製代碼
其實上面的案例可直接在父組件中for循環就好,此處只是做爲演示slot的做用域插槽; 實際開發中做用域插槽的使用場景主要爲:既能夠複用子組件的slot,又能夠使slot內容不一致。
vue2.0提供了$slot方法來訪問slot 此處代碼以**「具名slot(同時使用多個插槽)」**的代碼爲例,修改一會兒組件childComponment.vuethis
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)
}
};
複製代碼
打印結果:spa
其中elm的內容爲插槽內容,結構以下: