Vue child component calls the parent component's method and passes parameters
Vue is a popular front-end framework that provides many convenient functions and APIs, including componentization. In Vue, we can split a page into multiple small components, each with its own state and methods, which makes the code clearer and easier to maintain.
In Vue, the parent component can pass data to the child component through the props attribute, but how does the child component call the parent component's method and pass parameters? This requires the use of the event mechanism provided in Vue.
First, define a method in the parent component, for example:
<template>
<div>
<child-component @child-click="handleChildClick"></child-component>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
components: {
ChildComponent
},
methods: {
handleChildClick(data) {
console.log('child click:', data)
}
}
}
</script>
In the above code, we define a handleChildClick
method in the parent component, @child-click
listen to the click event of the child component through the event, and set the event callback function to handleChildClick
the method.
Next, trigger the parent component's method in the child component, for example:
<template>
<div>
<button @click="handleClick">Click me</button>
</div>
</template>
<script>
export default {
methods: {
handleClick() {
this.$emit('child-click', 'hello')
}
}
}
</script>
In the above code, we define a handleClick
method in the child component and this.$emit(‘child-click’, ‘hello’)
trigger an child-click
event called through and set the parameter to 'hello'.
Finally, we can get the parameters passed by the child component in the handleChildClick method of the parent component, for example:
handleChildClick(data) {
console.log('child click:', data) // Output:child click: hello
}
In the above code, we handleChildClick
output the parameter passed by the child component, namely 'hello', in the method of the parent component.
In summary, the method for a child component to call a parent component method and pass parameters is to trigger a custom event in the child component, set the event callback function to the method in the parent component, and then listen to the event through the $emit method in the parent component and obtain the parameters passed by the child component. This can achieve communication between the child component and the parent component.
For example, suppose we have a parent component and a child component. There is a method called handleChildClick in the parent component and a button in the child component. Clicking the button needs to trigger the handleChildClick method of the parent component and pass a parameter. The code is as follows:
Parent component code:
<template> <div> <child-component @child-click="handleChildClick"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, methods: { handleChildClick(data) { console.log('child click:', data) // Here you can do some processing, such as updating the status of the parent component this.parentData = data } }, data() { return { parentData: '' } } } </script>
Subcomponent code:
<template> <div> <button @click="handleClick">Click me</button> </div> </template> <script> export default { methods: { handleClick() { this.$emit('child-click', 'hello') } } } </script>
In the above code, we define a button in the child component and trigger an child-click
event called in the button's click event, and set the parameter to 'hello'.
In the parent component, we define a handleChildClick
method named , and @child-click
listen to the click event of the child component through the event, and set the event callback function to handleChildClick
the method. In handleChildClick
the method, we can get the parameters passed by the child component and do some processing, such as updating the status of the parent component.
Finally, we can get the parameters passed by the child component in the parent component and save them in parentData.
Summarize
In Vue, a child component can call a parent component method and pass parameters by triggering a custom event in the child component, setting the event callback function to the method in the parent component, and then $emit
listening to the event through the method in the parent component and obtaining the parameters passed by the child component. This allows communication between the child component and the parent component.
Through the above examples, we can see how to implement Vue in which child components call parent component methods and pass parameters. This method can well solve the communication problem between components, making the code clearer and easier to maintain.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Configuring Apache Web Server on Ubuntu and Debian
Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM
-
This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
My understanding of webservice is this
Publish Date:2025/03/18 Views:150 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:62 Category:NETWORK
-
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio
How to redirect a website from HTTP to HTTPS
Publish Date:2025/03/16 Views:117 Category:NETWORK
-
HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi
How to avoid soft 404 in the website during SEO
Publish Date:2025/03/17 Views:136 Category:NETWORK
-
This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand
Performance differences between HTTP REST API and WebSocket REST API
Publish Date:2025/03/17 Views:161 Category:NETWORK
-
What is a REST API? REST or RESTful API design (Representational State Transfer) is an architectural style that uses existing protocols. There are six key constraints for REST API design:
Webpack packages ES6 and CommonJs mixed React
Publish Date:2025/03/02 Views:179 Category:React
-
This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.
Details that need to be paid attention to when compiling react with webpack
Publish Date:2025/03/02 Views:201 Category:React
-
It is very convenient to compile and package react using webpack. Both need to be installed using npm. However, if you do not pay attention to the installation location of webpack and react parser babel during use, problems will occur during