vue插槽slot的使用示例

這篇文章將為大家詳細(xì)講解有關(guān)vue插槽slot的使用示例,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站重做改版、白銀網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為白銀等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

vue中插槽的使用非常廣泛,本文就插槽的使用和理解簡(jiǎn)單總結(jié)。

從字面理解插槽是預(yù)先插入一個(gè)代碼空間,用于后期塞入數(shù)據(jù)。

插槽分類(lèi)

匿名插槽     ------------------   匿名的代碼空間

具名插槽     ------------------   帶有命名的代碼空間

作用域插槽 -------------------   帶有數(shù)據(jù)的代碼空間

插槽使用示例

匿名插槽

說(shuō)明在組件中先定義預(yù)留的代碼空間,組件在使用時(shí)直接寫(xiě)入代碼

<template>
 <div class="child">
  <h4>這里是子組件</h4>
  <slot></slot>
 </div>
</template>

使用:

<template>
 <div class="father">
  <h4>這里是父組件</h4>
  <child>
   <div class="tmpl">
    <span>菜單1</span>
    <span>菜單2</span>
    <span>菜單3</span>
    <span>菜單4</span>
    <span>菜單5</span>
    <span>菜單6</span>
   </div>
  </child>
 </div>
</template>

具名插槽

預(yù)先在組件中定義一個(gè)帶有名稱(chēng)的代碼空間,使用組件時(shí)用:slot綁定名稱(chēng)

<template>
 <div class="child">
 // 具名插槽
 <slot name="up"></slot>
 <h4>這里是子組件</h4>
 // 具名插槽
 <slot name="down"></slot>
 // 匿名插槽
 <slot></slot>
 </div>
</template>

使用:

<template>
 <div class="father">
 <h4>這里是父組件</h4>
 <child>
  //插槽up
  <div class="tmpl" slot="up">
  <span>菜單1</span>
  <span>菜單2</span>
  <span>菜單3</span>
  <span>菜單4</span>
  <span>菜單5</span>
  <span>菜單6</span>
  </div>
  //插槽down
  <div class="tmpl" slot="down">
  <span>菜單-1</span>
  <span>菜單-2</span>
  <span>菜單-3</span>
  <span>菜單-4</span>
  <span>菜單-5</span>
  <span>菜單-6</span>
  </div>
  //匿名插槽
  <div class="tmpl">
  <span>菜單->1</span>
  <span>菜單->2</span>
  <span>菜單->3</span>
  <span>菜單->4</span>
  <span>菜單->5</span>
  <span>菜單->6</span>
  </div>
 </child>
 </div>
</template>

作用域插槽 (有數(shù)據(jù),但放開(kāi)了渲染)

在組件中預(yù)先定義一個(gè)帶有數(shù)據(jù)資源的代碼空間,使用組件時(shí)可以直接使用代碼空間中的數(shù)據(jù)

定義

<template>
 <div class="child">
 
 <h4>這里是子組件</h4>
 // 作用域插槽
 <slot :data="data"></slot>
 </div>
</template>
 export default {
 data: function(){
  return {
  data: ['zhangsan','lisi','wanwu','zhaoliu','tianqi','xiaoba']
  }
 }
}

使用

<template>
 <div class="father">
 <h4>這里是父組件</h4>
 <!--第一次使用:用flex展示數(shù)據(jù)-->
 <child>
  <template slot-scope="user">
  <div class="tmpl">
   <span v-for="item in user.data">{{item}}</span>
  </div>
  </template>
 
 </child>
 
 <!--第二次使用:用列表展示數(shù)據(jù)-->
 <child>
  <template slot-scope="user">
  <ul>
   <li v-for="item in user.data">{{item}}</li>
  </ul>
  </template>
 
 </child>
 
 <!--第三次使用:直接顯示數(shù)據(jù)-->
 <child>
  <template slot-scope="user">
  {{user.data}}
  </template>
 
 </child>
 
 <!--第四次使用:不使用其提供的數(shù)據(jù), 作用域插槽退變成匿名插槽-->
 <child>
  我就是模板
 </child>
 </div>
</template>

匿名插槽和具名插槽的功能是 預(yù)留插入代碼的空間;

作用域插槽是提供數(shù)據(jù)資源,預(yù)留代碼渲染邏輯空間

關(guān)于“vue插槽slot的使用示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)頁(yè)標(biāo)題:vue插槽slot的使用示例
本文網(wǎng)址:http://muchs.cn/article32/phospc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)商城網(wǎng)站、微信小程序、軟件開(kāi)發(fā)

廣告

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

成都seo排名網(wǎng)站優(yōu)化