From 2d3b24fc31ca506edfbac3ba8cc49556af9f9cc0 Mon Sep 17 00:00:00 2001 From: linzhe141 <1572213544@qq.com> Date: Tue, 3 Sep 2024 11:16:33 +0800 Subject: [PATCH 1/3] fix(runtime-core): reset current node when the KeepAlive slots default is null --- packages/runtime-core/src/components/BaseTransition.ts | 1 + packages/runtime-core/src/components/KeepAlive.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/runtime-core/src/components/BaseTransition.ts b/packages/runtime-core/src/components/BaseTransition.ts index a31f28b2388..a76570afb2b 100644 --- a/packages/runtime-core/src/components/BaseTransition.ts +++ b/packages/runtime-core/src/components/BaseTransition.ts @@ -224,6 +224,7 @@ const BaseTransitionImpl: ComponentOptions = { if (!(instance.job.flags! & SchedulerJobFlags.DISPOSED)) { instance.update() } + leavingHooks.afterLeave = undefined } return emptyPlaceholder(child) } else if (mode === 'in-out' && innerChild.type !== Comment) { diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index f897f40375f..07ac2ed2d78 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -267,6 +267,7 @@ const KeepAliveImpl: ComponentOptions = { pendingCacheKey = null if (!slots.default) { + current = null return null } From 3cb3ef7dae7e059eef3e2c301af6932f1534f6eb Mon Sep 17 00:00:00 2001 From: linzhe141 <1572213544@qq.com> Date: Tue, 3 Sep 2024 11:21:18 +0800 Subject: [PATCH 2/3] chore: update --- packages/runtime-core/src/components/BaseTransition.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/runtime-core/src/components/BaseTransition.ts b/packages/runtime-core/src/components/BaseTransition.ts index a76570afb2b..a31f28b2388 100644 --- a/packages/runtime-core/src/components/BaseTransition.ts +++ b/packages/runtime-core/src/components/BaseTransition.ts @@ -224,7 +224,6 @@ const BaseTransitionImpl: ComponentOptions = { if (!(instance.job.flags! & SchedulerJobFlags.DISPOSED)) { instance.update() } - leavingHooks.afterLeave = undefined } return emptyPlaceholder(child) } else if (mode === 'in-out' && innerChild.type !== Comment) { From 93be5857880ab4630ebd0472ea5d35f94d09d874 Mon Sep 17 00:00:00 2001 From: linzhe141 <1572213544@qq.com> Date: Tue, 3 Sep 2024 14:05:28 +0800 Subject: [PATCH 3/3] chore: update test --- .../runtime-dom/__tests__/createApp.spec.ts | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/runtime-dom/__tests__/createApp.spec.ts b/packages/runtime-dom/__tests__/createApp.spec.ts index 9a8d05df2df..0c6479e7ba2 100644 --- a/packages/runtime-dom/__tests__/createApp.spec.ts +++ b/packages/runtime-dom/__tests__/createApp.spec.ts @@ -1,4 +1,13 @@ -import { createApp, h } from '../src' +import { + KeepAlive, + Transition, + createApp, + h, + nextTick, + onDeactivated, + onUnmounted, + ref, +} from '../src' describe('createApp for dom', () => { // #2926 @@ -39,4 +48,55 @@ describe('createApp for dom', () => { // ensure no mutation happened to Root object expect(originalObj).toMatchObject(Root) }) + + test('Transition + KeepAlive', async () => { + const deactivated = vi.fn() + const unmounted = vi.fn() + const CompA = { + name: 'CompA', + setup() { + return () => h('h1', 'CompA') + }, + } + const CompB = { + name: 'CompB', + setup() { + onDeactivated(deactivated) + onUnmounted(unmounted) + return () => h('h1', 'CompB') + }, + } + + const current = ref('CompA') + const cacheList = ref(['CompA', 'CompB']) + const App = createApp({ + setup() { + return () => + h( + Transition, + { mode: 'out-in' }, + { + default: () => [ + h( + KeepAlive, + { + include: cacheList.value, + }, + [current.value === 'CompA' ? h(CompA) : h(CompB)], + ), + ], + }, + ) + }, + }) + App.mount(document.createElement('div')) + current.value = 'CompB' + await new Promise(resolve => setTimeout(resolve, 500)) + await nextTick() + cacheList.value.splice(1) + current.value = 'CompA' + await nextTick() + expect(deactivated).toHaveBeenCalledTimes(1) + expect(unmounted).toHaveBeenCalledTimes(1) + }) })