通过总线机制完成相应的功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="handelClick">{{selfContent}}</div>',
methods: {
handelClick: function () {
this.bus.$emit('change', this.selfContent)
}
},
mounted: function () {
var this_ = this
this.bus.$on('change', function (msg) {
this_.selfContent = msg
})
}
})
var vm = new Vue({
el: '#app',
})
</script>
</body>
</html>