Files
WAI_Project_UNIX/cool/hooks/parent.ts
2025-08-06 10:18:17 +08:00

23 lines
437 B
TypeScript

import { getCurrentInstance } from "vue";
/**
* 获取父组件
* @param name 组件名称
* @example useParent<ClFormComponentPublicInstance>("cl-form")
* @returns 父组件
*/
export function useParent<T>(name: string): T | null {
const { proxy } = getCurrentInstance()!;
let p = proxy?.$parent;
while (p != null) {
if (p.$options.name == name) {
return p as T | null;
}
p = p.$parent;
}
return p as T | null;
}