From bfa084c56dd5a7e85c3cfa5770468dd25c061b80 Mon Sep 17 00:00:00 2001 From: BeiYuShuiGuoLvDeKongQi <958414905@qq.com> Date: Fri, 31 Jul 2020 01:25:10 +0800 Subject: [PATCH 1/2] fix(keep-alive): call activated hook of nested components.(#1742) --- .../__tests__/components/KeepAlive.spec.ts | 30 +++++++++++++++++++ packages/runtime-core/src/renderer.ts | 4 ++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts index f75bb039c4e..8eee2c05e6b 100644 --- a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts +++ b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts @@ -165,6 +165,36 @@ describe('KeepAlive', () => { assertHookCalls(two, [1, 1, 2, 2, 0]) }) + // #1742 + test('should call lifecycle hooks on nested components when root component no hooks', async () => { + const two = { + name: 'two', + data: () => ({ msg: 'two' }), + render(this: any) { + return h('div', this.msg) + }, + activated: jest.fn() + } + const one = { + name: 'one', + data: () => ({ msg: 'one' }), + render(this: any) { + return h(two) + } + } + + const toggle = ref(true) + const App = { + render() { + return h(KeepAlive, () => (toggle.value ? h(one) : null)) + } + } + render(h(App), root) + + expect(serializeInner(root)).toBe(`
two
`) + expect(two.activated).toHaveBeenCalledTimes(1) + }) + test('should call correct hooks for nested keep-alive', async () => { const toggle2 = ref(true) one.render = () => h(KeepAlive, () => (toggle2.value ? h(two) : null)) diff --git a/packages/runtime-core/src/renderer.ts b/packages/runtime-core/src/renderer.ts index ed832253182..0587aaa0d2f 100644 --- a/packages/runtime-core/src/renderer.ts +++ b/packages/runtime-core/src/renderer.ts @@ -1264,7 +1264,7 @@ function baseCreateRenderer( if (!instance.isMounted) { let vnodeHook: VNodeHook | null | undefined const { el, props } = initialVNode - const { bm, m, a, parent } = instance + const { bm, m, parent } = instance if (__DEV__) { startMeasure(instance, `render`) } @@ -1323,6 +1323,8 @@ function baseCreateRenderer( }, parentSuspense) } // activated hook for keep-alive roots. + // fix 1742, delay the assignment + const { a } = instance if ( a && initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE From 219ee8b9822b6a4ac9cf7fb0a55c38029e490e51 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 6 Aug 2020 09:37:38 -0400 Subject: [PATCH 2/2] Update renderer.ts --- packages/runtime-core/src/renderer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/renderer.ts b/packages/runtime-core/src/renderer.ts index 0587aaa0d2f..5bdc0aa1fff 100644 --- a/packages/runtime-core/src/renderer.ts +++ b/packages/runtime-core/src/renderer.ts @@ -1323,7 +1323,8 @@ function baseCreateRenderer( }, parentSuspense) } // activated hook for keep-alive roots. - // fix 1742, delay the assignment + // #1742 activated hook must be accessed after first render + // since the hook may be injected by a child keep-alive const { a } = instance if ( a &&