Files
jindengchen-ai-report/cool-unix/cool/hooks/parent.ts

23 lines
437 B
TypeScript
Raw Normal View History

2025-11-13 10:36:23 +08:00
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;
}