Skip to content

Commit 1ea2400

Browse files
authored
fix(keep-alive): should remove wrapped version of injected keep alive hooks (#1959)
1 parent a47626a commit 1ea2400

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

packages/runtime-core/src/apiLifecycle.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function injectHook(
1818
hook: Function & { __weh?: Function },
1919
target: ComponentInternalInstance | null = currentInstance,
2020
prepend: boolean = false
21-
) {
21+
): Function | undefined {
2222
if (target) {
2323
const hooks = target[type] || (target[type] = [])
2424
// cache the error handling wrapper for injected hooks so the same hook
@@ -47,6 +47,7 @@ export function injectHook(
4747
} else {
4848
hooks.push(wrappedHook)
4949
}
50+
return wrappedHook
5051
} else if (__DEV__) {
5152
const apiName = `on${capitalize(
5253
ErrorTypeStrings[type].replace(/ hook$/, '')

packages/runtime-core/src/components/KeepAlive.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,16 @@ function registerKeepAliveHook(
360360
}
361361

362362
function injectToKeepAliveRoot(
363-
hook: Function,
363+
hook: Function & { __weh?: Function },
364364
type: LifecycleHooks,
365365
target: ComponentInternalInstance,
366366
keepAliveRoot: ComponentInternalInstance
367367
) {
368-
injectHook(type, hook, keepAliveRoot, true /* prepend */)
368+
// injectHook wraps the original for error handling, so make sure to remove
369+
// the wrapped version.
370+
const injected = injectHook(type, hook, keepAliveRoot, true /* prepend */)
369371
onUnmounted(() => {
370-
remove(keepAliveRoot[type]!, hook)
372+
remove(keepAliveRoot[type]!, injected)
371373
}, target)
372374
}
373375

0 commit comments

Comments
 (0)