How to Communicate between Components in Vue.js
Tags: vue, webdev, javascript, intermediate
Published: 2021-04-24 15:06:27

Every framework has its own unique architecture but they have one thing in common. The entire page is divided into small components where the component has its own functionalities and UI. And sometimes, we need a way to communicate between these components.

Generally, we use Vuex *(State management tool)* to store the data and use it across components. But sometimes, we need a way to send the data from one component to another without using the Vuex store. In this article, we will learn the possible ways of achieving that.

There are *five* ways to send the data from one component to another:

####1. Using Props (Parent to Child Communication)
####2. Using Events (Child to Parent Communication)
####3. Using Event Bus (Communication between any components)
####4. Using provide/inject (Parent to Child Communication)
####5. Using this.$refs (Parent to Child Communication)

Let’s study them one by one and see how they work.

##1) Using Props (Communication from parent to child )
One can send data from parent-to-child components using props. In the below example, we are sending the data from `parent. vue` to `child. vue` via the prop `message`.

![](https://miro.medium.com/max/1050/1*vkMroMXITfeTT4-Sj4xhIw.png)<figcaption>Child Component</figcaption>

We have created a component that prints the `message`. We have defined a variable in props called `message` which we are rendering in the template.

![](https://miro.medium.com/max/1050/1*2Yruesi8xx3n3ia-rASyng.png)<figcaption>Parent Component</figcaption>

While we nest the child component inside our parent component, we are binding the data we want to send by using the directive `v-bind` and sending the data using props.

Any time the parent changes the prop, the new value is sent to the child and rerendered.
##2) Using Events (Communication from child to parent )
This is similar to props but we are sending the data from child to parent and instead of using v-bind, we are using directive v-on for capturing.

We have defined a variable in the child component which contains our message. We are sending the same to the parent component by firing an event from the child component.

`this.$emit(’name-of-the-event’, args1, args2, args3,...)` is a way to fire the event. This is captured on the parent component using directive `v-on`. Note that one should use the same event name they sent while capturing and can retrieve the arguments.

![](https://miro.medium.com/max/1050/1*HLNvfMV9rDlRfKvCZbfOUw.png)<figcaption>Child Component</figcaption>
![](https://miro.medium.com/max/1050/1*3UbkRaQ48uF5F27euSZawQ.png)<figcaption>Parent Component</figcaption>

Any time the message in the child component changes, the new value is sent to the parent and rerendered.

##3) Using Event Bus (Communication between any two components)

An event bus is used to communicate between any two components *(Components need not have a parent-child relationship)*. This can be used when one needs to manually listen for events on a component instance.

You can just send the data from one component using `this.$root.$emit(’name-of-emitter’, args1, args2, ...)` and is captured using the same name like this `this.$root.$on(’name-of-emitter’, args1, args2, ...)` in the other component.

![](https://miro.medium.com/max/1050/1*GrGTB5Iqoc4kvXuqrd6ShQ.png)<figcaption>Child Component</figcaption>

Note that we are using `mounted() {}` to capture the event in *component-two. vue*.

![](https://miro.medium.com/max/1050/1*Di37udfhpWNgT5F8L_o4UA.png)<figcaption>Parent Component</figcaption>

##4) Using Provide/Inject (Parent to child communication)

This is used in the structure where you have deeply nested components and you only need something from the parent component in the deeply nested child.

In that case, you still need to pass the prop down the whole component chain which might be annoying. For such cases, we can use the provide and inject pair.

Although the example we have here is very simple, we are still sending a message from parent to child and hence it works.
![](https://miro.medium.com/max/1050/1*5GcGIjGnAigB29rEB4GQ9g.png)

We are using our parent component as a provider, sending the data, and injecting it into the child component using the same name.

![](https://miro.medium.com/max/1050/1*RmHAwR9g-Tx2Y7xB0dv3Rg.png)<figcaption>Child Component</figcaption>

Parent components can serve as dependency providers for all its children, regardless of how deep the component hierarchy is.

##5) Using this.$refs (Parent to Child Communication)

This method is the least preferred way of making the communication but it is one of the options available.
Despite the other methods, sometimes you might still need to directly access a child component. To achieve this you can assign a reference ID to the child component using the `ref` attribute.

![](https://miro.medium.com/max/1050/1*V1KCRVj4sfbjmf3Cd8aJfA.png)<figcaption>Parent Component</figcaption>

Here we are referencing the child component as *‘child’* (`ref= child`) (reference ID) and then capturing it using `this.$refs.child`. We can access anything related to the child component in this way.

![](https://miro.medium.com/max/1050/1*ll5GqlQ51uZ2xonYqffTgQ.png)<figcaption>ChildComponent</figcaption>

![](https://miro.medium.com/max/1500/1*pnRPnsSSv895CmErpaTBCw.png)

##Conclusion:
And that sums it up. Comment below if you know other ways of communication between components in Vue.js. Thank you.