vue技术文档react & vue & angular

vue学习(40)全局事件总线

2022-04-01  本文已影响0人  哆啦C梦的百宝箱
全局事件总线.png
思路
Vue.prototype.x=123;
const Demo = Vue.extend({});
const demo = new Demo();
Vue.prototype.x = demo;

尝试2:$on这些方法组件实例身上有的根本原因是在于这些方法在Vue构造函数的原型对象上面。所以如果x的值是Vue的实例对象也是可以的。在main.js中刚好有new这个实例对象,所以我们尝试写一下

const vm = new Vue({
  render: h => h(App),
}).$mount('#app');
Vue.prototype.x = demo;

执行代码发现报错,因为去定义x的时候,app已经挂载完成,子组件在mounted中给x绑定事件,就已经晚了。
尝试3:使用钩子

new Vue({
  render: h => h(App),
  beforeCreate(){
    //this本来就是这个vue实例,况且这里写vm也不行,妙
    Vue.prototype.x = this;
  }
}).$mount('#app');

备注:x一般叫$bus,因为bus刚好有总线的含义。

知识点
  1. 一种组件间的通信方式,适用于任意组件间通信。
  2. 安装全局事件总线
new Vue({
  ....
  Vue.prototype.$bus = this;
  .....
})
  1. 使用事件总线
    1. 如果A组件想接受数据,那么就给$bus定义一个自定义事件,并把回调写在A中。
    {
    ....
      mounted(){
        this.$bus.$on('peiqi',getData)
      }
      methods:{
        getData(data){.....}
      }
    ....
    }
    
    1. 提供数据this.$bus.emit('peiqi',666)
  2. 最好在beforeDestroy钩子中,用$off解绑当前组件所用到的事件。
上一篇 下一篇

猜你喜欢

热点阅读