1,参考例子,非父子组件之间的通讯 ,两个child组件之间通过bus总线来完成
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<child content="hello"></child>
<child content="world"></child>
</div>
<script>
Vue.prototype.bus=new Vue();
Vue.component('child',{
data:function () {
return {
selfContent:this.content
}
},
props:{
content:String
},
template:'<div @click="handleclick">{{selfContent}}</div>',
methods:{
handleclick:function () {
this.bus.$emit('change',this.selfContent)
}
},
mounted:function () {
var that=this;
this.bus.$on('change',function (msg) {
that.selfContent=msg
})
}
})
var app=new Vue({
el:"#app",
data:{
}
})
</script>
</body>
</html> |