[Vuejs]-Continuously propagate event up chain

1👍

You have multiple options here:

  1. You can use this.$root.$emit and then event will be sent to all components at once, you can listen to it as this.$root.$on
  2. You can create eventBus as explained here and then use it wherever you need to:
// The most basic event bus

// Imprt vue.js
import Vue from 'vue';

// Create empty vue.js instance to use as event bus
const Bus = new Vue({});

// Export event bus instance
export default Bus;
// Using the most basic event bus

import Vue from 'vue';
import Bus from './basic';

Vue.component('my-first-component', {
  methods: {
    sampleClickAction() {
       Bus.$emit('my-sample-event');
    }
  }
});

Vue.component('my-second-component', {
  created() {
    Bus.$on('my-sample-event', () => {
      // Do something…
    });
  }
});

Leave a comment