Detailed explanation of the case of Vue child component calling parent component method

Detailed explanation of the case of Vue child component calling parent component method

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:
  • Detailed example of how to change the value of a child component by triggering an event in the Vue parent component
  • Vue parent component calls child component function implementation
  • Detailed explanation of the event of triggering child components by clicking buttons in parent components in Vue
  • How to call child component functions in vue parent component
  • Vue3.0 triggers the parent component function in the child component

<<:  Causes and solutions to the garbled character set problem in MySQL database

>>:  Linux Jenkins configuration salve node implementation process diagram

Recommend

Detailed explanation of MySQL sql_mode query and setting

1. Execute SQL to view select @@session.sql_mode;...

How to create a Pod in Kubernetes

Table of contents How to create a Pod? kubectl to...

How to use the Linux basename command

01. Command Overview basename - strip directories...

Detailed explanation of 4 common data sources in Spark SQL

Generic load/write methods Manually specify optio...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...

KVM virtualization installation, deployment and management tutorial

Table of contents 1.kvm deployment 1.1 kvm instal...

A brief analysis of kubernetes controllers and labels

Table of contents 01 Common controllers in k8s RC...

Zen coding resource update function enhancement

Official website: http://code.google.com/p/zen-cod...

Summary of two methods to implement vue printing function

Method 1: Install the plugin via npm 1. Install n...

A brief description of the relationship between k8s and Docker

Recently, the project uses kubernetes (hereinafte...

Layui implements sample code for multi-condition query

I recently made a file system and found that ther...

Sample code using vue-router in html

Introducing vue and vue-router <script src=&qu...

MySQL Full-text Indexing Guide

Full-text indexing requires special query syntax....