Skip to content

Commit

Permalink
fix(keep-alive): should remove wrapped version of injected keep alive…
Browse files Browse the repository at this point in the history
… hooks (#1959)
  • Loading branch information
zhangzhonghe committed Aug 25, 2020
1 parent a47626a commit 1ea2400
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/runtime-core/src/apiLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +47,7 @@ export function injectHook(
} else {
hooks.push(wrappedHook)
}
return wrappedHook
} else if (__DEV__) {
const apiName = `on${capitalize(
ErrorTypeStrings[type].replace(/ hook$/, '')
Expand Down
8 changes: 5 additions & 3 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 1ea2400

Please sign in to comment.