国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

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

2019-11-19 12:32:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

什么是插槽?

插槽(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 樹組合在一起。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 樹組合在一起。

怎么用插槽?

默認(rèn)插槽

父組件

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

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

子組件(slotOne1)

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

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

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

父組件

<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>啦啦啦,啦啦啦,我是賣報(bào)的小行家</p>  <template slot="header">   <p>我是name為header的slot</p>  </template>  <p slot="footer">我是name為footer的slot</p> </slot-two> </div></template>

在父組件中使用template并寫入對(duì)應(yīng)的slot值來(lái)指定該內(nèi)容在子組件中現(xiàn)實(shí)的位置(當(dāng)然也不用必須寫到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>我不是賣報(bào)的小行家</slot> </div></template>

可以在子組件的slot標(biāo)簽中寫入內(nèi)容,當(dāng)父組件沒(méi)有寫入內(nèi)容時(shí)會(huì)顯示子組件的默認(rèn)內(nèi)容,當(dāng)父組件寫入內(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ì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 花莲县| 安新县| 富川| 汾阳市| 万全县| 隆回县| 龙井市| 洛隆县| 正定县| 读书| 台南市| 将乐县| 梅河口市| 温泉县| 紫阳县| 浮梁县| 浑源县| 青州市| 孟村| 广河县| 米泉市| 黎城县| 连云港市| 贺州市| 平果县| 桂平市| 丹棱县| 吴忠市| 阳泉市| 乃东县| 奉新县| 沂南县| 天镇县| 四会市| 神池县| 邵武市| 当涂县| 南阳市| 定南县| 四平市| 南涧|