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
1. Execute SQL to view select @@session.sql_mode;...
Table of contents How to create a Pod? kubectl to...
I developed a project some time ago. I used the f...
01. Command Overview basename - strip directories...
Generic load/write methods Manually specify optio...
Table of contents 1. Background 2. Local custom i...
Table of contents 1.kvm deployment 1.1 kvm instal...
Table of contents 1. Download MySQL msi version 2...
Table of contents 01 Common controllers in k8s RC...
Official website: http://code.google.com/p/zen-cod...
Method 1: Install the plugin via npm 1. Install n...
Recently, the project uses kubernetes (hereinafte...
I recently made a file system and found that ther...
Introducing vue and vue-router <script src=&qu...
Full-text indexing requires special query syntax....