From a6c0738c7029261424965ea92b888fdf8b9a98e5 Mon Sep 17 00:00:00 2001 From: MisicDemone <424532913@qq.com> Date: Mon, 21 Jun 2021 19:40:16 +0800 Subject: [PATCH] fix(vue): fix unmount a field in a wrong lifecycle function.(#1609) --- packages/vue/src/hooks/useAttach.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/vue/src/hooks/useAttach.ts b/packages/vue/src/hooks/useAttach.ts index 2ff8121faf4..bff3bd5b0c8 100644 --- a/packages/vue/src/hooks/useAttach.ts +++ b/packages/vue/src/hooks/useAttach.ts @@ -1,4 +1,4 @@ -import { onBeforeMount, onMounted, Ref, shallowRef, watch } from 'vue-demi' +import { onBeforeUnmount, onMounted, Ref, shallowRef, watch } from 'vue-demi' interface IRecycleTarget { onMount: () => void @@ -17,21 +17,24 @@ export const useAttach = ( target.onMount() }) - onBeforeMount(() => { + onBeforeUnmount(() => { oldTargetRef.value?.onUnmount() }) watch(dependencies, (cur, prev, onInvalidate) => { const target = creator() - if (oldTargetRef.value && target !== oldTargetRef.value) { - oldTargetRef.value.onUnmount() - } - oldTargetRef.value = target - target.onMount() - onInvalidate(() => { - oldTargetRef.value?.onUnmount() - }) + if (target !== oldTargetRef.value) { + if (oldTargetRef.value) { + oldTargetRef.value.onUnmount() + } + oldTargetRef.value = target + target.onMount() + + onInvalidate(() => { + oldTargetRef.value?.onUnmount() + }) + } }) return oldTargetRef