我定义了三个子组件,为什么点第一个和第二个,只有第三个会变;点第三个,前面两个不会变,搞不懂咩,哪个路过大哥能告诉我一下。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>非父子组件传值(Bus/总线/发布订阅模式/观察者模式)</title> <script src="./vue.js"></script> </head> <body> <div id="root"> <child content="xie"></child> <child content="Dell"></child> <child content="Lee"></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() { this_ = this this.bus.$on('change', function(msg) { this_.selfContent = msg }) } }) var vm = new Vue({ el: '#root', }) </script> </body> </html>