How to Update Parent Data from Child Component in VueJS?
Last Updated :
11 Sep, 2024
In Vue.js, communication between components is essential for building dynamic applications. One common scenario is updating the parent component's data from a child component. we will cover several ways to achieve this, including custom events, and v-model. Updating parent data from a child component allows the parent to react to changes in the child component. Vue.js offers various mechanisms to manage this communication efficiently, making it easier to maintain and scale applications.
These are the following ways to Update Parent Data from Child Component in VueJS:
Steps to Create the Vue.js Application
Step 1: Install Vue CLI
npm install -g @vue/cli
Step 2: Create a New Vue Project
vue create parent-child-communication-gfg-nikunj
Step 3: Navigate to the Project Directory
cd parent-child-communication-gfg-nikunj
Step 4: Run the Project
npm run serve
Project Structure:
Project structureUpdated Dependencies:
{
"dependencies": {
"core-js": "^3.6.5",
"vue": "^2.6.11"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-service": "~4.5.0",
"babel-eslint": "^10.1.0"
}
}
Using Custom Events
One of the most common ways to update parent data from a child component is through custom events. Vue allows a child component to emit events that the parent can listen for, which is ideal when the child component needs to send data back to the parent. In the child component, you emit an event using the $emit method, and in the parent component, you listen for the event using v-on or @ shorthand.
Example: This example demonstrates how the parent component updates its parentMessage data when the child component emits an event.
JavaScript
<script>
import ChildComponent from './ChildComponent.vue';
export default {
data() {
return {
parentMessage: '',
};
},
methods: {
handleUpdate(message) {
this.parentMessage = message;
},
},
components: {
ChildComponent,
},
};
// Contributed by Nikunj Sonigara
</script>
<template>
<div>
<h1>Parent Component</h1>
<p>Message from child: {{ parentMessage }}</p>
<ChildComponent @updateMessage="handleUpdate" />
</div>
</template>
JavaScript
<script>
export default {
data() {
return {
childMessage: '',
};
},
methods: {
sendUpdate() {
this.$emit('updateMessage', this.childMessage);
},
},
};
</script>
<template>
<div>
<h2>Child Component</h2>
<input type="text" v-model="childMessage" />
<button @click="sendUpdate">Send to Parent</button>
</div>
</template>
Output:
Using Custom EventsUsing v-model Directive
The v-model directive in Vue is commonly used for two-way data binding with form inputs. It can also be used between a parent and child component to establish two-way binding for any prop. This approach allows the parent to pass data to the child and the child to update the data directly. In the parent component, you bind the v-model directive to a variable. The child component will receive the prop (modelValue) and emit an update:modelValue event when the data changes.
Example: This example demonstrates how the parent component’s data is updated in real time as the child component modifies the bound value through v-model.
JavaScript
<script>
import ChildComponent from './ChildComponent.vue';
export default {
data() {
return {
parentMessage: '',
};
},
components: {
ChildComponent,
},
};
</script>
<template>
<div>
<h1>Parent Component</h1>
<p>Message from child: {{ parentMessage }}</p>
<ChildComponent v-model="parentMessage" />
</div>
</template>
JavaScript
<script>
export default {
props: {
modelValue: String,
},
computed: {
childMessage: {
get() {
return this.modelValue;
},
set(value) {
this.$emit('update:modelValue', value);
},
},
},
};
// Contributed by Nikunj Sonigara
</script>
<template>
<div>
<h2>Child Component</h2>
<input type="text" v-model="childMessage" />
</div>
</template>
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read