//Vue非父子组件间的传值
<body>
<div id="app">
<child content="L"></child>
<child content="Y"></child>
</div>
<script>
Vue.prototype.bus = new Vue();
Vue.component('child',{
props: {
content: String
},
data: function(){
return {
selfContent: this.content
}
},
template: "<div @click='handleClick'>{{selfContent}}</div>",
methods: {
handleClick: function(){
this.bus.$emit('change',this.selfContent);
}
},
mounted: function(){
var this_=this;
this.bus.$on('change',function(content){
this_.selfContent=content;
})
}
})
var vm = new Vue({
el: '#app'
})
</script>
</body>