new Vue({
el:'#app',
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this
},
});
Vue.prototype.$bus = new Vue({
el:'#app',
render: h => h(App),
});
第一种是教程里面的第二种是我自己想的,两种都能在vue的对象里面看见$bus这个对象,但是我这种在教程代码里就报错
mounted() {
console.log('School',this)
this.$bus.$on('hello',(data)=>{
console.log('我是School组件,收到了数据',data)
})
},
beforeDestroy() {
this.$bus.$off('hello')
},
肯定报错啊。你把vue实例化赋值给了$bus.vue都没实例化和挂载
用法不对,参考这边文章试试:《vue数据传递方式总结》 vue数据传递方式总结_蓑笠翁-CSDN博客_vue数据传输方式 目录 https://blog.csdn.net/m0_46309087/article/details/109689866?utm_source=app&app_version=4.9.1&code=app_1562916241&uLinkId=usr1mkqgl919blen