The vue3.0 parent component uses the value in the child component and calls the method in the child component

The vue3.0 parent component uses the value in the child component and calls the method in the child component


html:
use ref to define a variable mychildExpertsConfiguration

<template>
	<ExpertsConfiguration 
	   ref="mychildExpertsConfiguration"
	 ></ExpertsConfiguration>
 </template>

js:
Define a variable name mychildExpertsConfiguration in js that is the same as that defined by ref in html for binding, then mychildExpertsConfiguration.value can get all the values ​​and methods of the subcomponent, and mychildExpertsConfiguration.value?.XXX can get the details of the subcomponent value, mychildExpertsConfiguration.value?.XXX() to call the child component method

import { ref,reactive,onMounted,onBeforeUnmount } from 'vue';
import ExpertsConfiguration from "../ExpertsConfiguration/ExpertsConfiguration.vue" //引用子组件
export default {
    name: "GetMyTaskWaitingPageList",
    setup() {
    	//获取子组件里面的提交方法
        const mychildExpertsConfiguration = ref()
         //在本页面中调用
         const onSubmit= () => {
           	// console.log(mychildExpertsConfiguration.value,"所有子组件的值和方法")
            mychildExpertsConfiguration.value?.onAssessSubmit && mychildExpertsConfiguration.value.onAssessSubmit()
        }
        onMounted(() => {})
        return {
            mychildExpertsConfiguration,
            onSubmit
        }
    },
    components: {
        ExpertsConfiguration,
    }
}

Guess you like

Origin blog.csdn.net/Sunshinedada/article/details/128041824