欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue組件化中slot的基本使用方法

 更新時(shí)間:2019年05月01日 09:15:37   作者:阿飛飛飛  
這篇文章主要給大家介紹了關(guān)于vue組件化中slot的基本使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

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ā)中作用域插槽的使用場(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é)果:

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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論