CNCEC_APP/uni_modules/uview-pro/libs/function/parent.ts

22 lines
848 B
TypeScript
Raw Normal View History

2026-03-25 14:54:15 +08:00
import { type ComponentInternalInstance, getCurrentInstance } from 'vue';
export function parent(componentName?: string, _instance: ComponentInternalInstance | null | undefined = null) {
const instance = _instance || getCurrentInstance();
let parent = instance && (instance.parent as ComponentInternalInstance | null | undefined);
while (parent) {
const name = (parent.type as any)?.name as string | undefined;
if (name === componentName) {
return parent;
}
parent = parent.parent;
}
return null;
}
export function parentData(componentName?: string, _instance: ComponentInternalInstance | null | undefined = null) {
const instance = _instance || getCurrentInstance();
const findParent = parent(componentName, instance);
return findParent ? findParent.exposed : null;
}