main.js
引入插件 Vue.use
const demo = Vue.extend({})
const d = new demo()
Vue.prototype.x =d
创建公共属性便于 VM、VC 调用
mounted(){
this.$bus.$on('hello',(data)=>{
console.log(‘收到的消息’,data)
})
}
调用
sendStudent(){this.$bus.$emit('hello',666)}
挂载总线
new Vue({
render:(h)=>h(App),
beforeCreate(){
Vue.prototype.$bus = this
}
})
设计模式
当使用前端总线的方式 命名不能有重名 否则会出现冲突 一般会在当前的项目中创建一个 config 文件 一个常量文件 用于存储当前文件的常量名 保障程序运行正常
同事在 调用的组件里 创建一个销毁
beforeDestroy(){ this.$bug.$off('hello') }
否则 组件销毁了 傀儡还在 占用vm资源