父组件Father.vue
<template>
<button @click="func">触发子组件中的方法</button>
<child ref="childRef" />
</template>
<script setup>
import Child from './Child.vue'
const childRef = ref(null)
const func= () => {
// 调用子组件的test方法,并传值
childRef.value.test("hello!");
}
</script>
子组件Child.vue
<template>
<div>我是子组件</div>
</template>
<script setup>
// 定义子组件的方法
const test= (value) => {
console.log(value)
}
// 暴露方法
defineExpose({
test
})
</script>