溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue組件化中slot如何用

發(fā)布時(shí)間:2022-05-05 16:44:18 來源:億速云 閱讀:249 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“vue組件化中slot如何用”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“vue組件化中slot如何用”文章能幫助大家解決問題。

前言

slot可以在子組件中開啟插槽,在父組件引用該組建時(shí),可以定義這個(gè)插槽內(nèi)要展現(xiàn)的功能或模塊。

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">
 <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(同時(shí)使用多個(gè)插槽)

給slot指定一個(gè)名稱,可以分發(fā)多個(gè)slot插槽,但是只能有一個(gè)無(wú)名slot;

父組件的slot插槽內(nèi)容,不寫slot="xxx"的都會(huì)插到子組件的無(wú)名slot中;

如果沒有指定無(wú)名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會(huì)被拋棄。

<template>
 <div class="home">
 <child-componment>
  <h2 slot="header">
  父組件的header
  </h2>
  <h7 slot="footer">父組件的footer</h7>
  
  <h7>父組件的無(wú)名slot-1</h7>
  <p>
  父組件的無(wú)名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">
  <!-- 如果沒有指定無(wú)名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會(huì)被拋棄 -->
  <slot>
  <span >子組件默認(rèn)無(wú)名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>

vue組件化中slot如何用

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">
  <!-- 如果沒有指定無(wú)名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">
 <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è)計(jì)模式與開發(fā)實(shí)戰(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>

其實(shí)上面的案例可直接在父組件中for循環(huán)就好,此處只是作為演示slot的作用域插槽;

實(shí)際開發(fā)中作用域插槽的使用場(chǎng)景主要為:既可以復(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é)果:

vue組件化中slot如何用

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

vue組件化中slot如何用

關(guān)于“vue組件化中slot如何用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI