]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(keep-alive): should remove wrapped version of injected keep alive hooks (#1959)
authorzhangzhonghe <38434641+zhangzhonghe@users.noreply.github.com>
Tue, 25 Aug 2020 14:02:39 +0000 (22:02 +0800)
committerGitHub <noreply@github.com>
Tue, 25 Aug 2020 14:02:39 +0000 (10:02 -0400)
packages/runtime-core/src/apiLifecycle.ts
packages/runtime-core/src/components/KeepAlive.ts

index 74a6ef770cdcf3e2833905bb498132366da351a0..5a24ec2da58d6530188fcc98de1b0642eb2192f8 100644 (file)
@@ -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$/, '')
index cc821650dab2a11ea82c2f193b6650f8a8bf8bd8..b32a6ca2795216370ec6e6fb90b2e1c7785757f2 100644 (file)
@@ -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)
 }