From 4ba646f2b6c1337755436e6cffcc089f203fc4f7 Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 10 Feb 2021 11:03:36 +0800 Subject: [PATCH] feat(@vant/use): useParent always return index (#8120) --- .../vant-use/src/useRelation/useParent.ts | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/vant-use/src/useRelation/useParent.ts b/packages/vant-use/src/useRelation/useParent.ts index af669b378..c1b4816be 100644 --- a/packages/vant-use/src/useRelation/useParent.ts +++ b/packages/vant-use/src/useRelation/useParent.ts @@ -1,4 +1,5 @@ import { + ref, inject, computed, onUnmounted, @@ -18,25 +19,25 @@ export function useParent(key: string | symbol) { const parent = inject | null>(key, null); if (parent) { - const instance = getCurrentInstance(); + const instance = getCurrentInstance()!; + const { link, unlink, internalChildren, ...rest } = parent; - if (instance) { - const { link, unlink, internalChildren, ...rest } = parent; + link(instance); - link(instance); + onUnmounted(() => { + unlink(instance); + }); - onUnmounted(() => { - unlink(instance); - }); + const index = computed(() => internalChildren.indexOf(instance)); - const index = computed(() => internalChildren.indexOf(instance)); - - return { - parent: rest, - index, - }; - } + return { + parent: rest, + index, + }; } - return {}; + return { + parent: null, + index: ref(-1), + }; }