本文實(shí)例講述了vue 使用插槽分發(fā)內(nèi)容操作。分享給大家供大家參考,具體如下:
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)平度免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
單個(gè)插槽
除非子組件模板包含至少一個(gè) <slot> 插口,否則父組件的內(nèi)容將會(huì)被丟棄。當(dāng)子組件模板只有一個(gè)沒(méi)有屬性的插槽時(shí),父組件傳入的整個(gè)內(nèi)容片段將插入到插槽所在的 DOM 位置,并替換掉插槽標(biāo)簽本身。
最初在 <slot> 標(biāo)簽中的任何內(nèi)容都被視為備用內(nèi)容。備用內(nèi)容在子組件的作用域內(nèi)編譯,并且只有在宿主元素為空,且沒(méi)有要插入的內(nèi)容時(shí)才顯示備用內(nèi)容。
例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 測(cè)試實(shí)例 - 單個(gè)插槽</title> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> </head> <body> <div id="example"> <div> <h2>我是父組件的標(biāo)題</h2> <my-component> <p>這是一些初始內(nèi)容</p> <p>這是更多的初始內(nèi)容</p> </my-component> </div> </div> var childNode = { //當(dāng)沒(méi)有<slot>時(shí),父組件的其他內(nèi)容不會(huì)顯示,當(dāng)有<slot>時(shí),要是父組件中的內(nèi)容不為空,<slot> //中的內(nèi)容就不會(huì)顯示 template: ` <div> <h3>我是子組件的標(biāo)題</h3> <slot> 只有在沒(méi)有要分發(fā)的內(nèi)容時(shí)才會(huì)顯示。 </slot> </div> `, }; // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', components: { 'my-component': childNode } }) </script> </body> </html>
具名插槽
<slot> 元素可以用一個(gè)特殊的特性 name 來(lái)進(jìn)一步配置如何分發(fā)內(nèi)容。多個(gè)插槽可以有不同的名字。具名插槽將匹配內(nèi)容片段中有對(duì)應(yīng) slot 特性的元素。
仍然可以有一個(gè)匿名插槽,它是默認(rèn)插槽,作為找不到匹配的內(nèi)容片段的備用插槽。如果沒(méi)有默認(rèn)插槽,這些找不到匹配的內(nèi)容片段將被拋棄。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 測(cè)試實(shí)例 - 具名插槽</title> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> </head> <body> <div id="example"> <app-layout> <h2 slot="header">這里可能是一個(gè)頁(yè)面標(biāo)題</h2> <p>主要內(nèi)容的一個(gè)段落。</p> <p>另一個(gè)主要段落。</p> <p slot="footer">這里有一些聯(lián)系信息</p> </app-layout> </div> <script> Vue.component('app-layout',{ template:'<div class="container">'+ '<header>'+ '<slot name="header"></slot>'+ '</header>'+ '<main>'+ '<slot></slot>'+ '</main>'+ '<footer>'+ '<slot name="footer"></slot>'+ '</footer>'+ '</div>' }) // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', }) </script> </body> </html>
作用域插槽
作用域插槽是一種特殊類型的插槽,用作一個(gè) (能被傳遞數(shù)據(jù)的) 可重用模板,來(lái)代替已經(jīng)渲染好的元素。
在子組件中,只需將數(shù)據(jù)傳遞到插槽,就像你將 prop 傳遞給組件一樣:
<div class="child"> <slot text="hello from child"></slot> </div>
在父級(jí)中,具有特殊特性 slot-scope 的 <template> 元素必須存在,表示它是作用域插槽的模板。slot-scope 的值將被用作一個(gè)臨時(shí)變量名,此變量接收從子組件傳遞過(guò)來(lái)的 prop 對(duì)象:
在 2.5.0+,slot-scope 能被用在任意元素或組件中而不再局限于 <template>。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 測(cè)試實(shí)例 - 作用域插槽</title> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> </head> <body> <div id="example"> <parent-com></parent-com> </div> <script> Vue.component('child-com',{ template:'' + '<ul>' + ' <slot name="child-ul" v-for="item in animal" v-bind:text="item.name"></slot>' + '</ul>', data:function(){ return { animal:[ {name:'大象'}, {name:'小狗'}, {name:'小貓'}, {name:'老虎'} ] } } }); //父組件 // 在父組件的模板里,使用一個(gè)Vue自帶的特殊組件<template> , // 并在該組件上使用scope屬性,值是一個(gè)臨時(shí)的變量,存著的是由子組件傳過(guò)來(lái)的 // prop對(duì)象,在下面的例子中我把他命名為props。 // 獲得由子傳過(guò)來(lái)的prop對(duì)象。這時(shí)候,父組件就可以訪問(wèn)子組件在自定義屬性上暴露的數(shù)據(jù)了。 Vue.component('parent-com',{ template:'' + '<div class="container">' + '<p>動(dòng)物列表</p>' + '<child-com>' + ' <template scope="props" slot="child-ul">' + ' <li class="child-ul">{{ props.text }}</li>' + ' </template>' + '</child-com>' + '</div>' }); // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', }) </script> </body> </html>
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
網(wǎng)站名稱:vue使用插槽分發(fā)內(nèi)容操作示例【單個(gè)插槽、具名插槽、作用域插槽】
標(biāo)題來(lái)源:http://sd-ha.com/article4/ihhjie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、域名注冊(cè)、小程序開(kāi)發(fā)、Google、企業(yè)建站、云服務(wù)器
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)