最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue中slot(插槽)的介紹與使用

 更新時(shí)間:2018年11月12日 16:21:31   作者:妖色調(diào)  
這篇文章主要給大家介紹了關(guān)于vue中slot(插槽)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

什么是插槽?

插槽(Slot)是Vue提出來(lái)的一個(gè)概念,正如名字一樣,插槽用于決定將所攜帶的內(nèi)容,插入到指定的某個(gè)位置,從而使模板分塊,具有模塊化的特質(zhì)和更大的重用性。插槽顯不顯示、怎樣顯示是由父組件來(lái)控制的,而插槽在哪里顯示就由子組件來(lái)進(jìn)行控制

Vue slot 原理

在web-components中有slot的概念,https://developers.google.com/web/fundamentals/web-components/shadowdom。

<slot> 元素

Shadow DOM 使用 <slot> 元素將不同的 DOM 樹(shù)組合在一起。Slot 是組件內(nèi)部的占位符,用戶可以使用自己的標(biāo)記來(lái)填充。

通過(guò)定義一個(gè)或多個(gè) slot,您可將外部標(biāo)記引入到組件的 shadow DOM 中進(jìn)行渲染。 這相當(dāng)于您在說(shuō)“在此處渲染用戶的標(biāo)記”。

注:Slot 是為網(wǎng)絡(luò)組件創(chuàng)建“聲明性 API”的一種方法。它們混入到用戶的 DOM 中,幫助對(duì)整個(gè)組件進(jìn)行渲染,從而將不同的 DOM 樹(shù)組合在一起。

怎么用插槽?

默認(rèn)插槽

父組件

<template>
 <div>
 我是父組件
 <slotOne1>
  <p style="color:red">我是父組件插槽內(nèi)容</p>
 </slotOne1>
 </div>
</template>

在父組件引用的子組件中寫(xiě)入想要顯示的內(nèi)容(可以使用標(biāo)簽,也可以不用)

子組件(slotOne1)

<template>
 <div class="slotOne1">
 <div>我是slotOne1組件</div>
 <slot></slot>
 </div>
</template>

在子組件中寫(xiě)入slot,slot所在的位置就是父組件要顯示的內(nèi)容

當(dāng)然再父組件引用的子組件中也可以寫(xiě)入其他組件

父組件

<template>
 <div>
 我是父組件
 <slotOne1>
  <p style="color:red">我是父組件插槽內(nèi)容</p>
  <slot-one2></slot-one2>
 </slotOne1>
 </div>
</template>

子組件(slotOne2)

<template>
 <div class="slotOne2">
 我是slotOne2組件
 </div>
</template>

具名插槽

子組件

<template>
 <div class="slottwo">
 <div>slottwo</div>
 <slot name="header"></slot>
 <slot></slot>
 <slot name="footer"></slot>
 </div>
</template>

在子組件中定義了三個(gè)slot標(biāo)簽,其中有兩個(gè)分別添加了name屬性header和footer

父組件

<template>
 <div>
 我是父組件
 <slot-two>
  <p>啦啦啦,啦啦啦,我是賣(mài)報(bào)的小行家</p>
  <template slot="header">
   <p>我是name為header的slot</p>
  </template>
  <p slot="footer">我是name為footer的slot</p>
 </slot-two>
 </div>
</template>

在父組件中使用template并寫(xiě)入對(duì)應(yīng)的slot值來(lái)指定該內(nèi)容在子組件中現(xiàn)實(shí)的位置(當(dāng)然也不用必須寫(xiě)到template),沒(méi)有對(duì)應(yīng)值的其他內(nèi)容會(huì)被放到子組件中沒(méi)有添加name屬性的slot中

插槽的默認(rèn)內(nèi)容

父組件

<template>
 <div>
 我是父組件
 <slot-two></slot-two>
 </div>
</template>

子組件

<template>
 <div class="slottwo">
 <slot>我不是賣(mài)報(bào)的小行家</slot>
 </div>
</template>

可以在子組件的slot標(biāo)簽中寫(xiě)入內(nèi)容,當(dāng)父組件沒(méi)有寫(xiě)入內(nèi)容時(shí)會(huì)顯示子組件的默認(rèn)內(nèi)容,當(dāng)父組件寫(xiě)入內(nèi)容時(shí),會(huì)替換子組件的默認(rèn)內(nèi)容

編譯作用域

父組件

<template>
 <div>
 我是父組件
 <slot-two>
  <p>{{name}}</p>
 </slot-two>
 </div>
</template>
<script>
export default {
 data () {
 return {
  name: 'Jack'
 }
 }
}
</script>

子組件

<template>
 <div class="slottwo">
 <slot></slot>
 </div>
</template>

作用域插槽

子組件

<template>
 <div>
 我是作用域插槽的子組件
 <slot :data="user"></slot>
 </div>
</template>

<script>
export default {
 name: 'slotthree',
 data () {
 return {
  user: [
  {name: 'Jack', sex: 'boy'},
  {name: 'Jone', sex: 'girl'},
  {name: 'Tom', sex: 'boy'}
  ]
 }
 }
}
</script>

在子組件的slot標(biāo)簽上綁定需要的值

父組件

<template>
 <div>
 我是作用域插槽
 <slot-three>
  <template slot-scope="user">
  <div v-for="item in user.data" :key="item.id">
  {{item}}
  </div>
  </template>
 </slot-three>
 </div>
</template>

在父組件上使用slot-scope屬性,user.data就是子組件傳過(guò)來(lái)的值

總結(jié)

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

相關(guān)文章

最新評(píng)論

吉林省| 莆田市| 确山县| 阜平县| 健康| 宜兴市| 石屏县| 鱼台县| 洛川县| 清涧县| 玉田县| 鄂州市| 吉水县| 双柏县| 高碑店市| 广昌县| 梅州市| 明光市| 昌平区| 梧州市| 辽宁省| 大余县| 盐源县| 陕西省| 六枝特区| 红桥区| 佛教| 福泉市| 桐柏县| 油尖旺区| 桐柏县| 托里县| 岳阳县| 杭州市| 伊宁市| 武冈市| 北票市| 海林市| 双鸭山市| 东港市| 明溪县|