v3中的defineExpose用来暴露子组件方法;
子组件暴露两个方法
defineExpose({
open, setData
})
父组件调用
sonRef.value.open();
sonRef.value.setData();
为什么不可以链式调用
sonRef.value.open().setData
---------------------------------------------------------------------------------------------------------------------------------
解决办法(hobeas大佬提供)
使用 getCurrentInstance
返回当前实例
<template>
<button @click="onClick">click</button>
<child ref="sonRef" />
</template>
<script setup>
import { ref } from 'vue'
import child from './child.vue'
const sonRef = ref(null)
const onClick = () => {
sonRef.value.open().setData()
sonRef.value.setData().open()
}
&l