欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

vue組件化中slot怎么用

這篇文章將為大家詳細(xì)講解有關(guān)vue組件化中slot怎么用,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司專注于額敏網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供額敏營銷型網(wǎng)站建設(shè),額敏網(wǎng)站制作、額敏網(wǎng)頁設(shè)計(jì)、額敏網(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>

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">
  <!-- 如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會被拋棄 -->
  <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ā)中作用域插槽的使用場景主要為:既可以復(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é)果:

vue組件化中slot怎么用

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

vue組件化中slot怎么用

關(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è)計(jì)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)
国产高清在线不卡一区| 好骚国产99在线中文| 中文字幕一二区在线观看| 男人大臿蕉香蕉大视频| 国产av熟女一区二区三区四区 | 美女露小粉嫩91精品久久久| 在线免费不卡亚洲国产| 麻豆在线观看一区二区| 好吊一区二区三区在线看| 国产午夜福利片在线观看| 在线观看那种视频你懂的| 搡老熟女老女人一区二区| 亚洲品质一区二区三区| 国产激情国产精品久久源| 日韩美成人免费在线视频| 久久99青青精品免费观看| 日本东京热加勒比一区二区| 在线观看免费无遮挡大尺度视频| 日韩一级免费中文字幕视频| 午夜精品国产精品久久久| 日韩欧美一区二区亚洲| 久久亚洲精品成人国产| 日韩欧美综合在线播放| 免费观看一级欧美大片| 亚洲熟女乱色一区二区三区| 亚洲熟女熟妇乱色一区| 九九九热在线免费视频| 亚洲精品一区二区三区免| 中文字日产幕码三区国产| 亚洲一区二区三区在线中文字幕| 国产传媒一区二区三区| 麻豆国产精品一区二区三区| 不卡在线播放一区二区三区| 国产综合香蕉五月婷在线| 国产日本欧美韩国在线| 亚洲a级一区二区不卡| 亚洲欧美中文日韩综合| 国产一区欧美一区二区| 日韩欧美在线看一卡一卡| 空之色水之色在线播放| 国产精品制服丝袜美腿丝袜|