Home> Web Front-end> Vue.js> body text

How does a vue subcomponent call the method of the parent component?

青灯夜游
Release: 2021-10-26 12:03:28
Original
124269 people have browsed it

Method: 1. Call the method of the parent component through "this.$parent.event" in the child component. 2. The child component uses "$emit" to trigger an event to the parent component, and the parent component can listen to this event. 3. The parent component passes the method to the child component, and the method can be called directly in the child component.

How does a vue subcomponent call the method of the parent component?

The operating environment of this tutorial: Windows 7 system, vue version 2.9.6, DELL G3 computer.

In Vue, the sub-component calls the parent component. Here are three methods for reference.

The first methodis directly in the sub-component. Call the method of the parent component through this.$parent.event

Parent component

 
Copy after login

Child component

 
Copy after login

The second methodis in the child Use$emitin the component to trigger an event to the parent component, and the parent component can listen to this event.

Parent component

 
Copy after login

Subcomponent

 
Copy after login

The third typeis that the parent component passes the method into the subcomponent, in the subcomponent Call this method directly

parent component

 
Copy after login

child component

 
Copy after login

[Related recommendations:vue.js tutorial]

The above is the detailed content of How does a vue subcomponent call the method of the parent component?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn