1. Call the parent component method directly through this.$parent.event in the child component<!-- Parent component --> <template> <div> <child></child> </div> </template> <script> import child from '~/components/dam/child'; export default { components: child }, methods: { fatherMethod() { console.log('test'); } } }; </script> <!-- Child Component --> <template> <div> <button @click="childMethod()">Click</button> </div> </template> <script> export default { methods: { childMethod() { this.$parent.fatherMethod(); } } }; </script> 2. Use $emit in the child component to trigger an event to the parent component, and the parent component listens to this event<!-- Parent component --> <template> <div> <child @fatherMethod="fatherMethod"></child> </div> </template> <script> import child from '~/components/dam/child'; export default { components: child }, methods: { fatherMethod() { console.log('test'); } } }; </script> <!-- Child Component --> <template> <div> <button @click="childMethod()">Click</button> </div> </template> <script> export default { methods: { childMethod () { this.$emit('fatherMethod'); } } }; </script> 3. The parent component passes the method to the child component and calls the method directly in the child component<!-- Parent component --> <template> <div> <child :fatherMethod="fatherMethod"></child> </div> </template> <script> import child from '~/components/dam/child'; export default { components: child }, methods: { fatherMethod() { console.log('test'); } } }; </script> <!-- Child Component --> <template> <div> <button @click="childMethod()">Click</button> </div> </template> <script> export default { props: { fatherMethod: { type: Function, default: null } }, methods: { childMethod () { if (this.fatherMethod) { this.fatherMethod(); } } } }; </script> This concludes this article about the detailed case of Vue child component calling parent component method. For more relevant Vue child component calling parent component method content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Causes and solutions to the garbled character set problem in MySQL database
>>: Linux Jenkins configuration salve node implementation process diagram
Table of contents Proxy forwarding rules The firs...
Concept introduction: We know that the redo log i...
Preface There is a misunderstanding about the max...
During the project, I started using the js reques...
<br />In order to clearly distinguish the ta...
Date-type single-row functions in MySQL: CURDATE(...
Table of contents Brief description: 1. Four char...
In the process of team development, it is essenti...
Elastic stack, commonly known as ELK stack, is a ...
In the course of work, you will encounter many ca...
This article shares the specific code of js to re...
This morning I planned to use Wampserver to build...
Today's campus recruitment written test requi...
When I was writing the login page today, I needed...
Recently, an online security scan found a vulnera...