vue實(shí)現(xiàn)的組件兄弟間通信功能示例
本文實(shí)例講述了vue實(shí)現(xiàn)的組件兄弟間通信功能。分享給大家供大家參考,具體如下:
兄弟組件間通信(event)
借助于一個(gè)公共的Vue的實(shí)例對象,不同的組件可以通過該對象完成事件的綁定和觸發(fā)
var bus = new Vue(); bus.$emit() bus.$on()
熊大想要發(fā)消息給熊二,
接收方(熊二):事件綁定
bus.$on('customEvent',function(msg){
//msg就是通過事件 傳遞來的數(shù)據(jù)
})
發(fā)送方(熊大):觸發(fā)事件
bus.$emit('customEvent',123);
練習(xí):
在熊二中 加上一個(gè)button,
點(diǎn)擊按鈕時(shí)告訴熊大:'快跑!'
接收方:事件綁定
發(fā)送方:觸發(fā)事件
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.bootcss.com/vue/2.0.1/vue.min.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<xiongda></xiongda>
<hr>
<xionger></xionger>
</div>
<script>
/*借助于一個(gè)公共的Vue的實(shí)例對象,不同的組件可以通過該對象完成事件的綁定和觸發(fā)*/
//new一個(gè)對象,兄弟間的通信,將借助他事件綁定和觸發(fā)來實(shí)現(xiàn)
var bus = new Vue();
//熊大發(fā)送消息給熊二
//xiongda組件
Vue.component("xiongda",{
methods:{
sendToXiongEr:function(){
//給熊二發(fā)送消息
//觸發(fā)msgToXiongEr事件
bus.$emit("msgToXiongEr","哈哈,光頭強(qiáng)來了");
}
},
template:`
<div>
<h1>我是熊大</h1>
<button @click="sendToXiongEr">Click Me</button>
</div>
`
})
//熊二組件
Vue.component("xionger",{
template:`
<div>
<h1>我是熊二</h1>
</div>
`,
mounted:function(){
// 給該組件綁定一個(gè)自定義事件和對應(yīng)的處理函數(shù)
//調(diào)用bus中的on方法
//事件的觸發(fā)一般是接收數(shù)據(jù)然后處理
var that = this;
bus.$on("msgToXiongEr",function(msg){
alert("自定義的事件觸發(fā),接收到的數(shù)據(jù)"+msg);
})
}
})
new Vue({
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測試,可得到如下運(yùn)行效果:

改版:熊大在input輸入數(shù)據(jù)傳遞給熊二
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.bootcss.com/vue/2.0.1/vue.min.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<xiongda></xiongda>
<hr>
<xionger></xionger>
</div>
<script>
/*借助于一個(gè)公共的Vue的實(shí)例對象,不同的組件可以通過該對象完成事件的綁定和觸發(fā)*/
//new一個(gè)對象,兄弟間的通信,將借助他事件綁定和觸發(fā)來實(shí)現(xiàn)
var bus = new Vue();
//熊大發(fā)送消息給熊二
//xiongda組件
Vue.component("xiongda",{
data:function(){
return {
xiongDaInput:""
}
},
methods:{
sendToXiongEr:function(){
//給熊二發(fā)送消息
//觸發(fā)msgToXiongEr事件
bus.$emit("msgToXiongEr",this.xiongDaInput);
this.xiongDaInput = "";
}
},
template:`
<div>
<h1>我是熊大</h1>
<input type="text" v-model="xiongDaInput"/>
<button @click="sendToXiongEr">Click Me</button>
</div>
`
})
//熊二組件
Vue.component("xionger",{
data:function(){
return{
recvMsgIs:[]
}
},
template:`
<div>
<h1>我是熊二</h1>
<p v-for="tmp in recvMsgIs">{{tmp}}</p>
</div>
`,
mounted:function(){
// 給該組件綁定一個(gè)自定義事件和對應(yīng)的處理函數(shù)
//調(diào)用bus中的on方法
//事件的觸發(fā)一般是接收數(shù)據(jù)然后處理
var that = this;
bus.$on("msgToXiongEr",function(msg){
//alert("自定義的事件觸發(fā),接收到的數(shù)據(jù)"+msg);
that.recvMsgIs.push(msg);
})
}
})
new Vue({
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
感興趣的朋友還可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼的運(yùn)行效果。
希望本文所述對大家vue.js程序設(shè)計(jì)有所幫助。
相關(guān)文章
vue 使用 sortable 實(shí)現(xiàn) el-table 拖拽排序功能
這篇文章主要介紹了vue 使用 sortable 實(shí)現(xiàn) el-table 拖拽排序功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
示例vue 的keep-alive緩存功能的實(shí)現(xiàn)
這篇文章主要介紹了示例vue 的keep-alive緩存功能的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
Vue實(shí)現(xiàn)push數(shù)組并刪除的例子
今天小編就為大家分享一篇Vue實(shí)現(xiàn)push數(shù)組并刪除的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue3與elementui封裝一個(gè)便捷Loading
這篇文章主要介紹了vue3與elementui封裝一個(gè)便捷Loading,,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

