Introduction
In this article, I would like to talk about the front-end framework, Vue js. It is not very difficult for those who are familiar with the concepts of Angular. In this article, we will learn about communication between components in Vue.js.
There are multiple ways for inter-component communication in Vue.js, as mentioned below.
- Using props
- Using event
- Using event bus to communicate between any component
The first way of communication from a child component to a parent component is using props.
Firstly, I am going to cast a light on props - What, why, and how to use props. Let's start with why we need props.
- Props are known as one-way communication. It means Props allow you to pass the data from the parent component to the child component.
- At any time, if the value will change in the parent component, then the new value is set to the child and rendered (behind the scenes, the replacement of the hot module will work for rendering without a refresh ).
Let me give an example of props.
Go and create a component called parent component (Parent.vue).
- <template>
- <div>
- <Child titleOfChild="Children Component"></Child>
- </div>
- </template>
- <script>
- // Path of child component
- import { Child } from "./child.vue";
- export default {
- name: 'Parent',
- components: {
- Child
- }
- }
- </script>
NOTE
We have the child component tag (<Child></Child>) and here, we are passing data to the child component as "titleOfChild".
Go and create a component called child component (Child.vue).
- <template>
- <div>
- <h1>{{titleOfChild}}</h1>
- </div>
- </template>
- <script>
- export default {
- name: 'Child',
- props: [
- // camelCase in JavaScript
- 'titleOfChild'
- ]
- }
- </script>
NOTE
In child component, we are using props to get the value ("titleOfChild") which is passing from the parent component and we can directly use this value in the template using an expression (double curly braces).
Props can be used in different ways like,
- Passing static or dynamic props
- Passing a Number
- Passing a Boolean
- Passing an Object
- Passing an Array
The second way of communication in Vue.js from parent component to child component is described below.
Now, I am going to show how we can pass data from a child component to the parent component.
For this, we need to emit an event.
For example,
STEP 1
Go and create a child component (Child.vue).
- <template>
- <div class="submit-btn">
- <p>
- <input type="submit" value="Submit" @click="callingParentComponentMethod">
- "OR"
- <input type="submit" value="Submit" v-on:click="callingParentComponentMethod">
- </p>
- </div>
- </template>
- <script>
- export default {
- name: "Child",
- methods: {
- // This method will call on click of submit button
- callingParentComponentMethod() {
- // emit the event and call the parent component
- this.$emit("setParentComponentDetails");
- }
- }
- // props: ["cTitle", "cAbout", "cHome"]
- };
- </script>
NOTE
In the template section, we are calling a method "callingParentComponentMethod" on the click of Submit.
You can call the method in two ways like @click or v-on: click .
Inside the script section, we are using the "this.$emit()" method to call the parent component.

Join the conversation! Your thoughts help the community grow.