1.在main.js中配置在vue的原型上添加全局事件总线
new Vue({
el: '#app',
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this
}
})
2.发送数据:
methods:{
sendmsg(){
this.$bus.$emit('send',"1111")
}
}
3.接收数据
mounted() {
this.$bus.$on('send',data=>{
console.log("组件2收到了数据",data)
})
},
beforeDestroy() {
this.$bus.$off('send')
}