From 1ea24000c8f062f3cdc773e8e254892a05ba0702 Mon Sep 17 00:00:00 2001 From: zhangzhonghe <38434641+zhangzhonghe@users.noreply.github.com> Date: Tue, 25 Aug 2020 22:02:39 +0800 Subject: [PATCH] fix(keep-alive): should remove wrapped version of injected keep alive hooks (#1959) --- packages/runtime-core/src/apiLifecycle.ts | 3 ++- packages/runtime-core/src/components/KeepAlive.ts | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/runtime-core/src/apiLifecycle.ts b/packages/runtime-core/src/apiLifecycle.ts index 74a6ef770cd..5a24ec2da58 100644 --- a/packages/runtime-core/src/apiLifecycle.ts +++ b/packages/runtime-core/src/apiLifecycle.ts @@ -18,7 +18,7 @@ export function injectHook( hook: Function & { __weh?: Function }, target: ComponentInternalInstance | null = currentInstance, prepend: boolean = false -) { +): Function | undefined { if (target) { const hooks = target[type] || (target[type] = []) // cache the error handling wrapper for injected hooks so the same hook @@ -47,6 +47,7 @@ export function injectHook( } else { hooks.push(wrappedHook) } + return wrappedHook } else if (__DEV__) { const apiName = `on${capitalize( ErrorTypeStrings[type].replace(/ hook$/, '') diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index cc821650dab..b32a6ca2795 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -360,14 +360,16 @@ function registerKeepAliveHook( } function injectToKeepAliveRoot( - hook: Function, + hook: Function & { __weh?: Function }, type: LifecycleHooks, target: ComponentInternalInstance, keepAliveRoot: ComponentInternalInstance ) { - injectHook(type, hook, keepAliveRoot, true /* prepend */) + // injectHook wraps the original for error handling, so make sure to remove + // the wrapped version. + const injected = injectHook(type, hook, keepAliveRoot, true /* prepend */) onUnmounted(() => { - remove(keepAliveRoot[type]!, hook) + remove(keepAliveRoot[type]!, injected) }, target) }