chore(Layout): use relation

This commit is contained in:
chenjiahan
2020-09-25 14:00:59 +08:00
parent 36239090b9
commit a1bca37a45
3 changed files with 33 additions and 24 deletions

View File

@@ -94,23 +94,35 @@ export function useChildren(key: string) {
};
}
export function useParent(key: string) {
const parent = inject<any | null>(key, null);
type ParentProvide<T> = T & {
link(child: InternalInstance): void;
unlink(child: InternalInstance): void;
children: PublicInstance[];
internalChildren: InternalInstance[];
};
export function useParent<T>(key: string) {
const parent = inject<ParentProvide<T> | null>(key, null);
if (parent) {
const instance = getCurrentInstance();
const index = computed(() => parent.internalChildren.indexOf(instance));
parent.link(instance);
if (instance) {
const { link, unlink, internalChildren, ...rest } = parent;
onUnmounted(() => {
parent.unlink(instance);
});
link(instance);
return {
parent,
index,
};
onUnmounted(() => {
unlink(instance);
});
const index = computed(() => internalChildren.indexOf(instance));
return {
parent: rest,
index,
};
}
}
return {};