Vue2.0 slot分發(fā)內(nèi)容與props驗(yàn)證的方法
使用一種方式混合父組件的內(nèi)容與子組件自己的模板,這個(gè)過(guò)程被稱為“內(nèi)容分發(fā)”。在子組件中使用特殊的<slot>元素作為內(nèi)容的插槽。
Slot分發(fā)內(nèi)容
概述:
簡(jiǎn)單來(lái)說(shuō),假如父組件需要在子組件內(nèi)放一些DOM,那么這些DOM是顯示、不顯示、在哪個(gè)地方顯示、如何顯示,就是slot分發(fā)負(fù)責(zé)的活。
默認(rèn)情況下
父組件在子組件內(nèi)套的內(nèi)容,是不顯示的。
例如代碼:
<div id="app"> <children> <span>12345</span> <!--上面這行不會(huì)顯示--> </children> </div> <script> var vm = new Vue({ el: '#app', components: { children: { //這個(gè)無(wú)返回值,不會(huì)繼續(xù)派發(fā) template: "<button>為了明確作用范圍,所以使用button標(biāo)簽</button>" } } }); </script>
顯示內(nèi)容是一個(gè)button按鈕,不包含span標(biāo)簽里面的內(nèi)容;
一、單個(gè)slot
在子組件模板中有slot標(biāo)簽,被視為備用內(nèi)容,在父組件不提供內(nèi)容的情況下使用。如果父組件提供內(nèi)容,則把整個(gè)內(nèi)容片段插入到slot所在的DOM位置,并替換掉slot標(biāo)簽本身。
子組件模板中沒(méi)有slot標(biāo)簽,父組件提供的內(nèi)容會(huì)被拋棄
如果替換的內(nèi)容較多,可以直接用一個(gè)template替換。
<div id="app"> <h2>自定義組件</h2> <custom> <!-- 當(dāng)卸載自定義標(biāo)簽之前的內(nèi)容,要混合子組件中的模板 --> <div>我是父組件提供的內(nèi)容,我的存在會(huì)替換子組件中slot標(biāo)簽內(nèi)的內(nèi)容</div> </custom> </div> <script type="text/javascript" src='https://i0.jrjimg.cn/zqt-red-1000/focus/focus2017YMZ/teamFrighting/js/vue.min.js'></script> <script> Vue.component('custom',{ template:` <div> <slot> <div>我備用內(nèi)容,如果子組件中有內(nèi)容會(huì)替換我哦~</div> </slot> </div> ` }) new Vue({ el:"#app" }) </script>
見(jiàn)證奇跡的時(shí)候到了,頁(yè)面上會(huì)顯示如下內(nèi)容
單個(gè)slot.png
二、有具體名稱的slot
<slot>元素可以用一個(gè)特殊的屬性name來(lái)配置如何分發(fā)內(nèi)容。
<div id="app"> <h2>自定義組件</h2> <custom> <!-- <div slot="one">我替換one</div> --> <div slot="three">我替換three</div> <div slot="two">我替換two</div> <span slot="two">我替換two</span> <div slot="one">我替換one</div> </custom> </div> <script type="text/javascript" src='https://i0.jrjimg.cn/zqt-red-1000/focus/focus2017YMZ/teamFrighting/js/vue.min.js'></script> <script> Vue.component('custom',{ template:` <div> <slot name="one"> <p>我是one</p> </slot> <slot name="two"> <p>我是two</p> </slot> <slot name="three"> <p>我是three</p> </slot> </div> ` }) new Vue({ el:"#app" }) </script>
你猜頁(yè)面上會(huì)顯示什么?猜對(duì)了我就告訴你-。-
具名slot.png
是不是被順序驚到了,這是因?yàn)轫?yè)面會(huì)根據(jù)子組件中slot的順序去替換內(nèi)容并渲染頁(yè)面。
可以使用一個(gè)匿名的slot,處理那些沒(méi)有對(duì)應(yīng)slot的內(nèi)容
<div id="app"> <h2>自定義組件</h2> <custom> <!-- <div slot="one">我替換one</div> --> <div slot="three">我替換three</div> <div slot="two">我替換two</div> <span slot="two">我替換two</span> <div slot="one">我替換one</div> <div>替換無(wú)名的slot</div> <div>替換無(wú)名的slot</div> <div>替換無(wú)名的slot</div> </custom> </div> <script type="text/javascript" src='https://i0.jrjimg.cn/zqt-red-1000/focus/focus2017YMZ/teamFrighting/js/vue.min.js'></script> <script> Vue.component('custom',{ template:` <div> <slot name="one"> <p>我是one</p> </slot> <slot name="two"> <p>我是two</p> </slot> <slot name="three"> <p>我是three</p> </slot> <slot> <p>我是無(wú)名的slot</p> </slot> </div> ` }) new Vue({ el:"#app" }) </script>
匿名的slot就會(huì)被那些沒(méi)有對(duì)應(yīng)slot的內(nèi)容替換。
匿名slot.png
三、編譯作用域
父組件模板的內(nèi)容在父組件作用域內(nèi)編譯
子組件模板的內(nèi)容在子組件作用域內(nèi)編譯
<div id="app"> <h2>自定義組件</h2> <custom> <!-- 渲染的數(shù)據(jù),是父組件中的數(shù)據(jù),如果想使用子組件中的數(shù)據(jù),就在子組件中建立自己的數(shù)據(jù) --> {{message}} </custom> </div> <script type="text/javascript" src='https://i0.jrjimg.cn/zqt-red-1000/focus/focus2017YMZ/teamFrighting/js/vue.min.js'></script> <script> Vue.component('custom',{ data(){ return { message:"我是子組件的數(shù)據(jù)" } }, template:` <div> <p>我是{{message}}</p> <slot> // 這的內(nèi)容會(huì)被父組件中內(nèi)容替換 <p> {{message}}</p> </slot> </div> ` }) new Vue({ el:"#app", data:{ message:"我是父組件的數(shù)據(jù)" } }) </script>
頁(yè)面渲染
編譯作用域.png
運(yùn)用了slot分發(fā),使我們對(duì)組件的應(yīng)用更加靈活。
單向數(shù)據(jù)流
數(shù)據(jù)從父組件傳遞給子組件,只能單項(xiàng)綁定。
在子組件內(nèi)不應(yīng)該修改父組件傳遞過(guò)來(lái)的數(shù)據(jù)。
改變prop的情況:
1.作為data中局部數(shù)據(jù)的初始值使用
2.作為子組件中computed屬性
props 驗(yàn)證
我們?cè)诟附M件給子組件傳值得時(shí)候,為了避免不必要的錯(cuò)誤,可以給prop的值進(jìn)行類型設(shè)定,讓父組件給子組件傳值得時(shí)候,更加準(zhǔn)確
props:{ propA:Number, 數(shù)值類型 propB:[String,Number], 多種類型 propC:{type:String,required:true}, 必填項(xiàng) propD:{type:Number,default:100}, 默認(rèn)是 propE:{typr:Number,default:function(){return 1000}} propF:{validator:function(value){return value>2}} 符合value>2的值 }
不明白,看如下案例,要求父組件給子組件傳值得時(shí)候
1、這個(gè)參數(shù)是必須傳的
2、值必須是數(shù)值類型的
3、默認(rèn)參數(shù)是10
Vue.component('custom-cmpontent',{ data(){ return { incrementCount:this.count //作為局部組件的data的初始值 } }, props:{ count:{ type:Number, // 類型 default:10, // 默認(rèn)值 required:true //必須要傳參數(shù) } }, computed:{ incrementCount2(){ return this.incrementCount } }, template:` <div> <h2>我是一個(gè)自定義組件</h2> <input type='button' value="改變count的值" @click="changeCount"> {{incrementCount}} </div> `, methods:{ changeCount:function(value){ this.incrementCount++; this.$emit('receive') } } }) new Vue({ el:"#app", data:{ count:0 }, methods:{ countH:function(){ this.count++; } } })
那如果我們給子組件傳值得時(shí)候,傳過(guò)去的是一個(gè)字符串,就會(huì)報(bào)錯(cuò)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue組件中slot的用法
- 深入理解vue中slot與slot-scope的具體使用
- 詳解vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot
- vue如何使用 Slot 分發(fā)內(nèi)容實(shí)例詳解
- Vue內(nèi)容分發(fā)slot(全面解析)
- vue Render中slots的使用的實(shí)例代碼
- 詳解Vue學(xué)習(xí)筆記入門篇之組件的內(nèi)容分發(fā)(slot)
- Vue.js中組件中的slot實(shí)例詳解
- 詳解vue slot插槽的使用方法
- Vue.js之slot深度復(fù)制詳解
- Vue中的slot使用插槽分發(fā)內(nèi)容的方法
相關(guān)文章
iView UI FORM 動(dòng)態(tài)添加表單項(xiàng)校驗(yàn)規(guī)則寫(xiě)法實(shí)例
這篇文章主要為大家介紹了iView UI FORM 動(dòng)態(tài)添加表單項(xiàng)校驗(yàn)規(guī)則寫(xiě)法實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01vue+springmvc導(dǎo)出excel數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue+springmvc導(dǎo)出excel數(shù)據(jù)的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06Vue3 Element-plus el-menu無(wú)限級(jí)菜單組件封裝過(guò)程
對(duì)于element中提供給我們的el-menu組件最多可以實(shí)現(xiàn)三層嵌套,如果多一層數(shù)據(jù)只能自己通過(guò)變量去加一層,如果加了兩層、三層這種往往是行不通的,所以只能進(jìn)行封裝,這篇文章主要介紹了Vue3 Element-plus el-menu無(wú)限級(jí)菜單組件封裝,需要的朋友可以參考下2023-04-04Vue使用mind-map實(shí)現(xiàn)在線思維導(dǎo)圖
Vue中的Mind-Map通常是指使用Vue.js這個(gè)前端框架構(gòu)建的思維導(dǎo)圖組件或庫(kù),它可以幫助開(kāi)發(fā)者在Web應(yīng)用中創(chuàng)建動(dòng)態(tài)、交互式的思維導(dǎo)圖,讓用戶可以直觀地組織信息和結(jié)構(gòu)化數(shù)據(jù),本文介紹了Vue使用mind-map實(shí)現(xiàn)在線思維導(dǎo)圖,需要的朋友可以參考下2024-07-07vue-video-player 解決微信自動(dòng)全屏播放問(wèn)題(橫豎屏導(dǎo)致樣式錯(cuò)亂問(wèn)題)
這篇文章主要介紹了vue-video-player 解決微信自動(dòng)全屏播放問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02vue前端實(shí)現(xiàn)驗(yàn)證碼登錄功能
這篇文章主要介紹了vue前端實(shí)現(xiàn)驗(yàn)證碼登錄功能,登錄時(shí)圖形驗(yàn)證通過(guò)三種方法結(jié)合實(shí)例代碼給大家講解的非常詳細(xì), 通過(guò)實(shí)例代碼介紹了vue登錄時(shí)圖形驗(yàn)證碼功能的實(shí)現(xiàn),感興趣的朋友一起看看吧2023-12-12