component.uts
762 Bytes
import type { ComponentPublicInstance, Ref } from "vue";
/**
* 组件实例 Ref 类型
* 用于简化组件 ref 的类型定义
*/
export type ComponentRef = ComponentPublicInstance | null;
/**
* 调用组件方法的辅助函数
* @param ref 组件 ref
* @param methodName 方法名
*/
export const callComponentMethod = (ref: Ref<ComponentRef>, methodName: string): void => {
if (ref.value != null) {
console.log(`[callComponentMethod] 调用方法: ${methodName}`);
try {
(ref.value as ComponentPublicInstance).$callMethod(methodName);
} catch (error) {
console.error(`[callComponentMethod] 调用失败: ${methodName}`, error);
}
} else {
console.warn(`[callComponentMethod] ref.value 为空,无法调用: ${methodName}`);
}
};