Sales3 min read508 words

Vue.js: Communication between components

Burak Kaşıkcı

PlusClouds Author

Cloud & SaaS

Vue.js: Communication Between Components

When developing an application with Vue.js, we separate all parts into individual components, building a structure on top of this. This not only provides us with a more organized structure but also prevents us from having to write the same code multiple times or making the same adjustments in dozens of places when a change is needed.

Of course, this is not specific to Vue.js or something suggested here, and there is a general software principle known as DRY (Don't Repeat Yourself) related to this topic. This principle is one of the fundamental principles of software and advocates that code duplication should be avoided. (It is named DRY and explained in depth in "The Pragmatic Programmer" book.)

By adhering to this principle and logic, we create components in our projects, which have dynamic data, and we need to pass this data from parent components to child components (parent to child), from child components to parent components (child to parent), and between lower-level components (child to child). Let's look at how we can perform these data transfer operations.

For Parent - Child communication: props

We use the props structure when transferring data from the parent component to the child component. We can use the data we send via props in the child component just like a value we define within data.

Parent Component

 
// Here, we are assigning a static value to the title.


// Here, we send the Title value defined in the item of the parent component to the title value defined in the child component.


We can use it as follows when receiving it.

Child Component


For Child - Parent communication: $emit


We had transferred data to the child via props from the parent; here we need to do the opposite and push the data upwards. For this, we use the emit structure. If we need to give a short example, we can use it like this: this.$emit("result", item)
Continuing from the above example, we will have the following result;

Child Component


To catch these emits in the parent, we use it as @emitName or v-on:emitName.
Parent Component


For Child - Child communication: event-bus


Here, since we do not have a direct relationship like parent-child, we need to use an intermediary, and we use the eventbus instance here. We create this instance as a Vue instance that does not bind to any DOM element, and we will only use this instance to transfer data. All components can send events here, and all components that listen here can take action according to the incoming data.

Here, we will first add an eventBus instance to the event-bus.js as mentioned.

Then it will be enough for us to import it in the pages we will use. We did not define it globally, as we will use it only between two components.

In our current example, let's update the title in Component A by clicking the button in Component B.

Child A Component

Child B Component

With the above 3 methods, we can carry out data transfers between components.
#There is no text provided for translation. Please provide the text you would like to be translated into English.

Frequently Asked Questions

How do you pass data from a parent component to a child component using props?

Use the props option to pass data from parent to child. For example, you can set title='First title' or bind with :title='item.Title'; in the child, you can use the prop as if it were part of its own data.

How can a child component send data back to its parent in Vue.js?

A child can emit an event using this.$emit('eventName', payload). This allows the parent to receive data back through the event handler.

What syntax does the parent component use to listen to a child's emitted event?

The parent listens with the syntax @eventName or v-on:eventName in the child’s usage. This connects the emitted data to a handler in the parent.

What is an event bus and when would you use it in Vue?

An event bus is a dedicated Vue instance that is not bound to any DOM element, used as an intermediary for components that do not have a direct parent-child relationship. It enables components to publish and listen for events to share data.

How do you set up and use an event bus for inter-component communication?

Create an eventBus instance in event-bus.js as a Vue instance; import it into the components that need it rather than making it global. Components can emit and listen to events via the eventBus to coordinate actions.

What three methods of data transfer between Vue components are described in the article?

Props are used for parent to child data transfer, this.$emit is used for child to parent communication, and an event bus is used for child to child communication when there is no direct relationship.