Vue.js global event bus

后端 未结 4 1623
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 04:40

I am trying to create a global event bus so that two sibling components can communicate with each other. I have searched around; however, I cannot find any examples of how t

4条回答
  •  傲寒
    傲寒 (楼主)
    2020-12-31 05:36

    This is answered long back, here is my solution using in vue.js-2

    main.js

    import Vue from 'vue'
    import App from './App'
    
    export const eventBus = new Vue({
      methods:{
        counter(num) {
          this.$emit('addNum', num);
        }
      }
    });
    
    new Vue({
      el: '#app',
      template: '',
      components: { App }
    });
    

    comp1.vue

    //Calling my named export
    import { eventBus } from '../../main'
    
    
    
    

提交回复
热议问题