Vue Event Bus to communicate sibling components

Last Updated: August 1, 2021

In this communication method, Vue.js instance can emit events from one component and another component can listen it.
This is very useful when you develop large Vue application because there are lot of components at different level. So when you change the data on one component other components should be notified

Files needed in this examaple

When I click the button on CompA incremented value will be shown in CompB

Add this line of code to main.js
Vue.prototype.$eventHub = new Vue();

First you create these functions in CompB because you are going to listen the events emited from CompA

 created() {
        this.$root.$on('change-name', this.changeName)

 beforeDestroy() {

In the method section of the CompA you can add the following code

changeName(name) {
// name will be automatically transported to the parameter. = name;

Then you can fire the events from CompB
this.$root.$emit(‘change-name’,”Mike Tyson”);