父组件
<template>
<Child ref="childRef"></Child>
</template>
<script setup lang="ts">
import Child from './Child .vue';
const childRef = ref(null);
const onParentClick = (): void => {
if (childRef.value) {
childRef.value?.onChildClick();
}
};
</script>
子组件
const onChildClick= (): void => {
console.log('父组件调用了子组件')
};
defineExpose({ onChildClick }); // 将方法暴露出去
参考资料:
vue3.0 父子组件通讯之调用方法,含setup语法糖(全网最详细)_vue3 setup语法糖 父子通信_Beast_H的博客-CSDN博客