DEV Community

communication between components in Vue

Paul Wanjohi on May 11, 2019

we will use this sandbox to illustrate communication between different components, feel free to fork and play with it. I used tailwind for my styli...
Collapse
 
dimpadev profile image
Dimitri Pandeleakis

Note that the "sibling to sibling communication" you stated in the article is actually a global event bus which is a simple way of getting unrelated sections of your application to talk to each other. However, it is also considered an anti-pattern if not used correctly and it's really hard to maintain a medium-large codebase using exclusively this architecture. My recommendation would be: adopt vuex for everything as a default, and fall back to a global event bus if you think vuex becomes a substantial overhead in simple apps. I would only use a global event bus for events that don't necessarily mutate state or application data but only in rare cases/scenarios.
While it seems easier to set up and integrate a global event bus for "communicating between distant components", you will inevitably regret it once your app has achieved a certain size and level of complexity, especially once you need to refactor some of your views.

Collapse
 
paulwvnjohi profile image
Paul Wanjohi

thanks for highlighting that

Collapse
 
sotaan profile image
David Mamane

Don't forget to unbind the handler before destroying the component.
In your case:

beforeDestroy() {
eventBus.$off('ageWasEdited');
}

Also, you'd better refactor your handler in a method to unbind it specifically.