Vue parent component calls child component function implementation

Vue parent component calls child component function implementation

Vue parent component calls the function of the child component

The parent component calls the child component's function through the event. For example, the parent component allows the child component to send a request through a click event.

The project in the article has been created through scaffolding.

DEMO:

Father.js
<template>
    <div>
        <div>
            <son ref="son"></son>
            <input type="button" value="click" @click="useSonFun">
        </div>  
    </div>
</template>

<script>
import son from './son'
export default {
    components:{
        son
    },
    data(){
        return {
            
        }
    },
    methods: {
        useSonFun(){
            this.$refs.son.say(); //Give the child component a ref and call the function in the child component through ref}
    },
}
</script>
Son.js
<template>
    <div>
        <h1>SON</h1>
    </div>
</template>

<script>
export default {
    data(){
        return {
            
        }
    },
    methods:{
        say(){//Subcomponent function that needs to be called by the parent component console.log("SON COMPONENT");
        }
    },
}
</script>

Rendering

insert image description here

The parent component calls the child component function. You can use it to send a request by clicking on the parent component. Based on the click event, the child component also sends a request. It can be distinguished from the parent component clicking to send a request, obtaining data, and then passing the data to the child component through the component value passing method.

Tips:

Father.js:
this.$refs.son.say (the parent component data can be passed to the child component in brackets);
Son.js:
say(receive data from parent component to child component){
	//Use of data}

This is the end of this article about the introduction and use of the observer pattern in Java design pattern. For more relevant observer pattern content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the case of Vue child component calling parent component method
  • Detailed example of how to change the value of a child component by triggering an event in the Vue parent component
  • 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

<<:  Detailed explanation of the failure of MySQL to use UNION to connect two queries

>>:  Tutorial on installing MySQL 8.0.11 using RPM on Linux (CentOS7)

Recommend

MySQL 8.0.20 installation tutorial and detailed tutorial on installation issues

Original address: https://blog.csdn.net/m0_465798...

my.cnf (my.ini) important parameter optimization configuration instructions

MyISAM storage engine The MyISAM storage engine i...

Detailed explanation of the implementation of MySQL auto-increment primary key

Table of contents 1. Where is the self-incremente...

Complete steps for Docker to pull images

1. Docker pull pulls the image When using $ docke...

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

Solution to transparent font problem after turning on ClearType in IE

The solution to the transparent font problem after...

A brief discussion on Mysql specified order sorting query

Recently, I have been working on a large-screen d...

Vue implements real-time refresh of the time display in the upper right corner

This article example shares the specific code of ...

Detailed explanation of Vue's methods and properties

Vue methods and properties 1. Methods Usage 1 met...

MySQL database introduction: detailed explanation of database backup operation

Table of contents 1. Single database backup 2. Co...

Best Practices for MySQL Upgrades

MySQL 5.7 adds many new features, such as: Online...

Summary of 10 common HBase operation and maintenance tools

Abstract: HBase comes with many operation and mai...

WeChat applet canvas implements signature function

In the WeChat applet project, the development mod...