// 父组件
<template>
<button @click="fun">调用子组件方法</button>
<child ref="childRef"></child>
</template>
<script setup>
//引入
import { ref } from "vue";
import child from "./child.vue";
//与ref一致
const childRef = ref(null);
const fun = () => {
childRef.value.childFun();// 调用子组件的方法
}
</script >
// 子组件
<script setup>
import { defineExpose } from 'vue'
const childFun = () => {
console.log('我是子组件方法')
}
// 注意!!这里必须用defineExpose暴露出去**
defineExpose({
childFun
})
</script>