父组件内容:
<template>
<child-component ref="childRef"></child-component>
<template>
<script setup lang="ts">
const childRef = ref(null);
const triggerChildMethod = () => {
if (childRef.value) {
// 通过 $refs 访问子组件实例并调用其方法
childRef.value.childMethod();
}
};
const handleMethod = () =>{
triggerChildMethod()
}
</script>
子组件内容:
<script setup lang="ts">
const childMethod = () => {
// 此处写需要实现的内容
}
defineExpose({ childMethod })
</script>