事件总线

一、安装全局事件总线:

首先在main.js中要定一个x,$bus作为数据中转中心

new vue({

beforCreate(){

  this.prototype.$bus=this

})

 

二、在School.vue中注册一个事件:

mounted(){

  this.$bus.$on('hello',(data)=>{

  console.log('我是School组件,收到了数据',data)

}),

//需要解绑

beforeDestory(){

  this.$bus.$off('hello')

}

}

三、在兄弟组件Student.vue中调用hello方法,并传递数据

methods(){

  sendstudentname(){

  this.$emit("hello',name)

}

}

 

posted @ 2021-12-17 20:10  浩鑫  阅读(85)  评论(0)    收藏  举报