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 various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

Undo log in MySQL

Concept introduction: We know that the redo log i...

How to test the maximum number of TCP connections in Linux

Preface There is a misunderstanding about the max...

Using CSS3 to achieve progress bar effect and dynamically add percentage

During the project, I started using the js reques...

HTML table tag tutorial (44): table header tag

<br />In order to clearly distinguish the ta...

Detailed explanation of single-row function code of date type in MySQL

Date-type single-row functions in MySQL: CURDATE(...

In-depth explanation of MySQL isolation level and locking mechanism

Table of contents Brief description: 1. Four char...

How to build a multi-node Elastic stack cluster on RHEL8 /CentOS8

Elastic stack, commonly known as ELK stack, is a ...

Talk about implicit conversion in MySQL

In the course of work, you will encounter many ca...

Notes on upgrading to mysql-connector-java8.0.27

Recently, an online security scan found a vulnera...