1、直接上代码,子组件名为conform.vue,代码内容如下:
<template>
<br/><span>{{ref_validate}}</span>
</template>
<script setup>
import { ref } from 'vue'
const ref_validate = ref('验证ref在组建上,获取子组件中的函数')
const test = (args)=>{
console.log(args)
ref_validate.value = args['success']
}
defineExpose({ ref_validate, test })
</script>
<style scoped lang="less">
</style>
2、app.vue中的内容:
<template>
<button @click="validate_ref">主要按钮</button>
<conform ref="validate" ></conform>
</template>
<script setup>
import conform from './components/conform.vue'
import {nextTick, ref} from "vue";
const validate = ref(null)
const validate_ref = ()=>{
validate.value?.test({success:'验证成功了'});
console.log(validate.value?.ref_validate);
}
</script>
<style scoped lang="less">
</style>
3、结果:

点击button后

注意ref的另一种用法:请点击参看