1、第一步:在项目中的 main.js
初始化 $bus
new Vue({ render: h => h(App), // 定义全局事件总线 $bus beforeCreate () { Vue.prototype.$bus = this } }).$mount('#app')
2、发送参数
this.$bus.$emit('方法名', 参数或方法)
3、接收参数
接收参数 this.$bus.$on('方法名', (参数) => { this.msg = 参数 })
4、接收完后记得销毁(因为全局都可调用为避免出错接收完后一定要销毁)
this.$bus.$off('方法名')