typescript
/**
* 获取组件ref
* @param {VueComponentIns} 组件实例
* @returns 组件ref
*/
// eslint-disable-next-line
export function useCompRef<T extends abstract new (...args: any) => any>(_: T) {
return ref<InstanceType<T>>();
}
使用
html
<a-com ref="aComRef"></a-com>
<script lang="ts" setup>
import ACom from '@/components/a-com.vue';
//引入
import { useCompRef } from '@/sss/useRefs';
const aComRef = useCompRef(ACom);
//调用组件里的函数,比如说这个组件是一个弹窗,那么打开弹窗
const openCom = () => {
aComRef.value?.open();
};
</script>