From 820a14345798edc0ab673bae8ce3181e479d9cca Mon Sep 17 00:00:00 2001 From: ygj6 <7699524+ygj6@users.noreply.github.com> Date: Mon, 22 Nov 2021 15:50:19 +0800 Subject: [PATCH] fix(runtime-core): handle error in async KeepAlive hooks (#4978) --- .../__tests__/components/KeepAlive.spec.ts | 31 ++++++++++++++++++- .../runtime-core/src/components/KeepAlive.ts | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts index 4dce5d14..b60931e1 100644 --- a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts +++ b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts @@ -16,7 +16,9 @@ import { cloneVNode, provide, defineAsyncComponent, - Component + Component, + createApp, + onActivated } from '@vue/runtime-test' import { KeepAliveProps } from '../../src/components/KeepAlive' @@ -874,4 +876,31 @@ describe('KeepAlive', () => { await nextTick() expect(serializeInner(root)).toBe('
1
') }) + + // #4976 + test('handle error in async onActivated', async () => { + const err = new Error('foo') + const handler = jest.fn() + + const app = createApp({ + setup() { + return () => h(KeepAlive, null, () => h(Child)) + } + }) + + const Child = { + setup() { + onActivated(async () => { + throw err + }) + }, + render() {} + } + + app.config.errorHandler = handler + app.mount(nodeOps.createElement('div')) + + await nextTick() + expect(handler).toHaveBeenCalledWith(err, {}, 'activated hook') + }) }) diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index 30ad4400..223a7c60 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -381,7 +381,7 @@ function registerKeepAliveHook( } current = current.parent } - hook() + return hook() }) injectHook(type, wrappedHook, target) // In addition to registering it on the target instance, we walk up the parent