小編給大家分享一下vue如何實(shí)現(xiàn)父子組件之間的通信以及兄弟組件的通信功能示例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

Vue具體輕量級(jí)框架、簡(jiǎn)單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢(shì),Vue中頁(yè)面使用的是局部刷新,不用每次跳轉(zhuǎn)頁(yè)面都要請(qǐng)求所有數(shù)據(jù)和dom,可以大大提升訪問(wèn)速度和用戶體驗(yàn)。
具體如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>www.jb51.net 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測(cè)試上述代碼,可得如下運(yùn)行效果:

看完了這篇文章,相信你對(duì)“vue如何實(shí)現(xiàn)父子組件之間的通信以及兄弟組件的通信功能示例”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁(yè)標(biāo)題:vue如何實(shí)現(xiàn)父子組件之間的通信以及兄弟組件的通信功能示例-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://chinadenli.net/article30/dgphso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、品牌網(wǎng)站建設(shè)、定制網(wǎng)站、App設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)建站
聲明:本網(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)
猜你還喜歡下面的內(nèi)容