vue中父子組件注意事項(xiàng),傳值及slot應(yīng)用技巧
一.父子組件傳值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>父子組件傳值</title> <style> </style> <script src="./vue.js"></script> </head> <body> <div id="root"> <counter :count="0" @numberchange="handleChange"></counter> <counter :count="0" @numberchange="handleChange"></counter> <div>{{total}}</div> <validate-content content="hello world"></validate-content> </div> <script> //父組件向子組件傳值用 props ,加:號(hào)后傳遞的為js表達(dá)式,示例中則為數(shù)字,不加:號(hào)代表的是字符串 var counter = { //局部注冊(cè) props:['count'], data:function(){//在子組件中定義數(shù)據(jù),data不能是對(duì)象,必須是一個(gè)函數(shù)。 return { number:this.count } }, template:'<div @click="handleClick2">{{number}}</div>', methods:{ handleClick2:function(){ this.number ++; //this.count++; 父組件可以傳值給子組件,但子組件不可以修改父組件屬性,這里這么寫(xiě)會(huì)報(bào)錯(cuò)。 this.$emit("numberchange",this.number);//子組件向父組件傳遞事件,值 } } } var validateContent = { props:{ //content:[Number,String] //組件參數(shù)校驗(yàn),可以多選 content:{//組件參數(shù)校驗(yàn) type:String, required:true, default:"default value", validator:function(value){ return value.length > 5 } } }, template:'<div >{{content}}</div>', } var vm = new Vue({ el:'#root', data:{ total:0 }, methods:{ handleChange:function(number){ console.log(number) // this.total +=1; } }, components:{ counter, //局部注冊(cè)要在根節(jié)點(diǎn)注冊(cè)組件 validateContent } }) </script> </body> </html>
二.父組件向子組件傳遞DOM
先看一個(gè)示例
<body> <div id="root"> <child><p>Qin</p></child> </div> <script> let child = { template :`<div> <p>hello world</p> </div>` } var vm = new Vue({ el:'#root', components:{ child } }) </script> </body>
打開(kāi)查看器查看一下
發(fā)現(xiàn)Qin不見(jiàn)了
<p>Qin</p>1
查看官方文檔 , https://cn.vuejs.org/v2/guide/components-slots.html
我們得出結(jié)論:如果 child 沒(méi)有包含一個(gè) < slot > 元素,則任何傳入它的內(nèi)容都會(huì)被拋棄
我們加入插槽
<body> <div id="root"> <child><p>Qin</p></child> </div> <script> let child = { template :`<div> <p>hello world</p> <slot></slot> </div>` } var vm = new Vue({ el:'#root', components:{ child } }) </script> </body>
發(fā)現(xiàn)Qin能正常顯示,且slot將會(huì)被替換為解析后的片段 < p > Qin < /p >
當(dāng)父組件不向子組件傳值的時(shí)候,slot還可以作為父組件默認(rèn)值出現(xiàn)
<body> <div id="root"> <child></child> </div> <script> let child = { template :`<div> <p>hello world</p> <slot>default value</slot> </div>` } var vm = new Vue({ el:'#root', components:{ child } }) </script> </body>
效果圖
具名插槽
如果想使用多個(gè)插槽,我們先看看效果:
<body> <div id="root"> <child> <header>This is header</header> <footer>This is footer</footer> </child> </div> <script> let child = { template : `<div> <slot></slot> <p>Main content</p> <slot></slot> </div>` } var vm = new Vue({ el:'#root', components:{ child } }) </script> </body>
發(fā)現(xiàn)出現(xiàn)了多個(gè)header和footer,要解決這個(gè)問(wèn)題就要用到具名插槽
我們修改代碼如下:
<body> <div id="root"> <child> <header slot="header">This is header</header> <footer slot="footer">This is footer</footer> </child> </div> <script> let child = { template : `<div> <slot name="header"></slot> <p>Main content</p> <slot name="footer"></slot> </div>` } var vm = new Vue({ el:'#root', components:{ child } }) </script> </body>
可以看到顯示正常了
總結(jié)
以上所述是小編給大家介紹的vue中父子組件注意事項(xiàng),傳值及slot應(yīng)用技巧,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue項(xiàng)目實(shí)現(xiàn)左滑刪除功能(完整代碼)
最近在開(kāi)發(fā)移動(dòng)端項(xiàng)目,通過(guò)向左滑動(dòng)出現(xiàn)刪除按鈕,點(diǎn)擊即可刪除,怎么實(shí)現(xiàn)這個(gè)功能呢,接下來(lái)小編給大家?guī)?lái)實(shí)例代碼幫助大家學(xué)習(xí)vue項(xiàng)目實(shí)現(xiàn)左滑刪除功能,感興趣的朋友跟隨小編一起看看吧2021-05-05vue使用better-scroll實(shí)現(xiàn)下拉刷新、上拉加載
這篇文章主要為大家詳細(xì)介紹了vue使用better-scroll實(shí)現(xiàn)下拉刷新、上拉加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11詳解如何在Vue2中實(shí)現(xiàn)組件props雙向綁定
在Vue2中組件的props的數(shù)據(jù)流動(dòng)改為了只能單向流動(dòng),如何在Vue2中實(shí)現(xiàn)組件props雙向綁定 ,一起來(lái)跟小編看看。2017-03-03vue全局過(guò)濾器概念及注意事項(xiàng)和基本使用方法
這篇文章主要給大家分享了vue全局過(guò)濾器概念及注意事項(xiàng)和基本使用方法,下面文字圍繞vue全局過(guò)濾器的相關(guān)資料展開(kāi)具體的詳細(xì)內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-11-11vue從后臺(tái)渲染文章列表以及根據(jù)id跳轉(zhuǎn)文章詳情詳解
這篇文章主要給大家介紹了關(guān)于vue從后臺(tái)渲染文章列表以及根據(jù)id跳轉(zhuǎn)文章詳情的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12