這篇文章將為大家詳細(xì)講解有關(guān)vue插槽slot的使用示例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
vue中插槽的使用非常廣泛,本文就插槽的使用和理解簡(jiǎn)單總結(jié)。
從字面理解插槽是預(yù)先插入一個(gè)代碼空間,用于后期塞入數(shù)據(jù)。
插槽分類
匿名插槽 ------------------ 匿名的代碼空間
具名插槽 ------------------ 帶有命名的代碼空間
作用域插槽 ------------------- 帶有數(shù)據(jù)的代碼空間
插槽使用示例
匿名插槽
說明在組件中先定義預(yù)留的代碼空間,組件在使用時(shí)直接寫入代碼
<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è)帶有名稱的代碼空間,使用組件時(shí)用:slot綁定名稱
<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ù),但放開了渲染)
在組件中預(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í),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享名稱:vue插槽slot的使用示例-創(chuàng)新互聯(lián)
分享地址:http://chinadenli.net/article26/cdpdjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站制作、微信小程序、定制開發(fā)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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í)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容