vue中的兄弟传参EventBus小实例
eventbus事件车使用于两个组件之间的通信,方法是在vue的main.js中创建一个vue实例
Vue.prototype.EventBus = new Vue()
组件A
<button @click="EventBus.$emit('change','我是来自组件A')">传参按钮</button>
这里的change是个函数,后面的要是传递的参数
组件B
mounted () {
this.EventBus.$on('change',(val)=>{
console.log(val)
})
},
//监听函数change的变化,后边是个也是个函数用来接听传入的值
点击组件A页面的按钮B组件后台会打印“我是来自组件A”