feat(@vant/use): useParent always return index (#8120)

This commit is contained in:
neverland 2021-02-10 11:03:36 +08:00 committed by GitHub
parent faab7c03f0
commit 4ba646f2b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import { import {
ref,
inject, inject,
computed, computed,
onUnmounted, onUnmounted,
@ -18,25 +19,25 @@ export function useParent<T>(key: string | symbol) {
const parent = inject<ParentProvide<T> | null>(key, null); const parent = inject<ParentProvide<T> | null>(key, null);
if (parent) { if (parent) {
const instance = getCurrentInstance(); const instance = getCurrentInstance()!;
const { link, unlink, internalChildren, ...rest } = parent;
if (instance) { link(instance);
const { link, unlink, internalChildren, ...rest } = parent;
link(instance); onUnmounted(() => {
unlink(instance);
});
onUnmounted(() => { const index = computed(() => internalChildren.indexOf(instance));
unlink(instance);
});
const index = computed(() => internalChildren.indexOf(instance)); return {
parent: rest,
return { index,
parent: rest, };
index,
};
}
} }
return {}; return {
parent: null,
index: ref(-1),
};
} }