vue實(shí)現(xiàn)父子組件之間的通信以及兄弟組件的通信功能示例
本文實(shí)例講述了vue實(shí)現(xiàn)父子組件之間的通信以及兄弟組件的通信功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.dbjr.com.cn vue父子組件通信、兄弟組件通信</title> <style> *{ margin: 0; padding: 0; list-style: none; } table{ text-align: center; margin:0 auto; } div{ text-align: center; } </style> </head> <body> <div id="app"> <table border="1" cellpadding="0" cellspacing="0"> <tr><td colspan="3">父組件數(shù)據(jù)</td></tr> <tr><td>name</td><td>{{name}}{{ff()}}</td><td><input type="text" v-model="name"></td></tr> <tr><td>age</td><td>{{age}}{{ff()}}</td><td><input type="text" v-model="age"></td></tr> </table> <v-son :son-name="name" :son-age="age" @sza="gg"></v-son> </div> <template id="son"> <div> <button @click="sonChange">子組件按鈕</button> <table border="1" cellpadding="0" cellspacing="0"> <tr><td colspan="3">子組件數(shù)據(jù)</td></tr> <tr><td>name</td><td>{{sonName}}</td><td><input type="text" v-model="sonName"></td></tr> <tr><td>age</td><td>{{sonAge}}</td><td><input type="text" v-model="sonAge"></td></tr> </table> <g-son :g-name="sonName" :g-age="sonAge"></g-son> </div> </template> <template id="vgson"> <div> <button @click="gchan">孫子組件按鈕</button> <table border="1" cellpadding="0" cellspacing="0"> <tr><td colspan="3">孫子組件數(shù)據(jù)</td></tr> <tr><td>name</td><td>{{gName}}</td><td><input type="text" v-model="gName"></td></tr> <tr><td>age</td><td>{{gAge}}</td><td><input type="text" v-model="gAge"></td></tr> </table> </div> </template> </body> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <script> var bus=new Vue(); const app=new Vue({ el:"#app", data:{ name:"keep", age:"28" }, methods:{ gg(val1,val2){ this.name=val1 this.age=val2 }, ff(){ bus.$on("suibian", (val1,val2)=> { this.name=val1; this.age=val2 }) } }, components:{ "vSon":{ template:"#son", methods:{ sonChange(){ this.$emit("sza",this.sonName,this.sonAge) } }, props:["sonName","sonAge"], components:{ "gSon":{ template:"#vgson", props:["gName","gAge"], methods:{ gchan(){ bus.$emit("suibian",this.gName,this.gAge); }, } } }, } } }) </script> </html>
這里使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼,可得如下運(yùn)行效果:
希望本文所述對大家vue.js程序設(shè)計有所幫助。
相關(guān)文章
vue3的父傳子問題(單項(xiàng)數(shù)據(jù)流)
這篇文章主要介紹了vue3的父傳子問題(單項(xiàng)數(shù)據(jù)流),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能
這篇文章主要為大家詳細(xì)介紹了Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Vue包大小優(yōu)化的實(shí)現(xiàn)(從1.72M到94K)
這篇文章主要介紹了Vue包大小優(yōu)化的實(shí)現(xiàn)(從1.72M到94K),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02解決vue中菜單再次點(diǎn)擊內(nèi)容不刷新問題
當(dāng)elementUI中菜單打開后,再次點(diǎn)擊不會刷新的問題,導(dǎo)致菜單再次點(diǎn)擊不刷新的根本原因是頁面打開后,再次打開相同的頁面是不會刷新的,這應(yīng)該是框架的機(jī)制就是如此,小編整理了兩個比較不錯的解決方法,需要的朋友可以參考下2023-08-08Vue如何使用Element-ui表單發(fā)送數(shù)據(jù)與多張圖片到后端詳解
在做項(xiàng)目的時候遇到一個問題,前端需要上傳表單到后端,表單數(shù)據(jù)包括文本內(nèi)容和圖片,這篇文章主要給大家介紹了關(guān)于Vue如何使用Element-ui表單發(fā)送數(shù)據(jù)與多張圖片到后端的相關(guān)資料,需要的朋友可以參考下2022-04-04