<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>非父子组件间的传值(Bus/总线/发布订阅模式/观察者模式)</title>
<script src="./vue.js"></script>
<style>
.actived {
color: red;
}
</style>
</head>
<body>
<!-- 非父子组件之间的传值 也可以使用vuex -->
<div id="app">
<child content="Dell"></child>
<child content="Lee"></child>
</div>
<script>
Vue.prototype.bus = new Vue()
Vue.component('child', {
data: function() {
return {
selfCount: this.content
}
},
props:{
content: String
},
template: '<div @click="handleClick">{{selfCount}}</div>',
methods: {
handleClick: function() {
this.bus.$emit('change', this.selfCount)
}
},
mounted: function() {
var this_ = this
this.bus.$on('change', function(msg) {
this_.selfCount = msg
})
}
})
var vm = new Vue({
el: "#app",
})
</script>
</body>
</html>
非父子组件之间的传值
最新推荐文章于 2023-04-14 19:43:56 发布