Vue provides one excellent way and three other alternative ways of getting your components to work together to form one beautiful, seamless application. Let us look at them one by one :)
1. Vuex
Yesterday we talked about Vuex and how we (try to) manage vuex variables and local variables in a Vue component. What we did not talk about was how Vuex is the most effective solution for all inter-component communications.
Vuex provides a centralized storage that is easy to use, is modular, and can act as a repository for all states, methods and more in your application. You will benefit greatly and keep your application super DRY by evaluating whether your states / functions can be moved to Vuex.
Referring to Vuex state is also a clean affair.
|
|
Here users
is a module in our vuex store and holds all states, methods and beyond for user-related stuff. We refer to one such variable called userName
.
Set the Vuex states through mutations within this component or any other component in your app.
Component communication becomes extremely simple when using Vuex -
- Component A does stuff, updates a state in Vuex store
- Component B, C, or Z have access to the changed state
This communication is multi-directional, can be easily refactored and scaled for current and future components.
2. Props
Props to a component are what arguments are for a function. You can pass props to a component and the target component can make use of it.
The source component will pass variables and target component needs to have definitions of the passed variable.
|
|
And, the target is -
|
|
As you can make out, this sort of communication is limited in reach but useful for a more localised treatment than Vuex. The direction is always from the caller component (source) to the called component (target). Target has to make use of events to talk back to the source.
3. Events
Events can be used by a component to raise a user or system event to all its friends. The source will raise events, and any target components interested in the event will have to ‘catch’ them.
|
|
The “catcher” will have following code -
|
|
4. Event Bus
The last event in this post is the event bus. Instead of sending events that a distinct source has to raise and a lone target has to listen to, we make events reach multiple components.
The last example can be changed to -
|
|
$root
represents the root component in the application. We just use it as a channel to reach all interested components.
Any component that wants to listen to this component can have -
|
|
The component is registering a call-back telling $root
that it is interested to know if there is an event called raiseClickClicked
. mounted()
just happens to be a convenient place to call such a call back since the component comes into existence with mounted
. You could do that anywhere you please.
Final Statements
For inter-component communication -
- use Vuex.
- use Prop for extremely local communication, or where using a store may not be possible.
- use events only for small interactions like opening/closing dialogs.