From fc99e4d3f01b190ef9fd3c218a668ba9124a32bc Mon Sep 17 00:00:00 2001 From: edison Date: Mon, 15 Apr 2024 22:40:38 +0800 Subject: [PATCH] fix(Transition): ensure the KeepAlive children unmount w/ out-in mode (#10632) close #10620 --- .../components/BaseTransition.spec.ts | 37 +++++++++++++++++++ .../runtime-core/src/components/KeepAlive.ts | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/components/BaseTransition.spec.ts b/packages/runtime-core/__tests__/components/BaseTransition.spec.ts index 7c389fe1ede..3184c9c9c6a 100644 --- a/packages/runtime-core/__tests__/components/BaseTransition.spec.ts +++ b/packages/runtime-core/__tests__/components/BaseTransition.spec.ts @@ -7,6 +7,7 @@ import { h, nextTick, nodeOps, + onUnmounted, ref, render, serialize, @@ -768,6 +769,42 @@ describe('BaseTransition', () => { test('w/ KeepAlive', async () => { await runTestWithKeepAlive(testOutIn) }) + + test('w/ KeepAlive + unmount innerChild', async () => { + const unmountSpy = vi.fn() + const includeRef = ref(['TrueBranch']) + const trueComp = { + name: 'TrueBranch', + setup() { + onUnmounted(unmountSpy) + const count = ref(0) + return () => h('div', count.value) + }, + } + + const toggle = ref(true) + const { props } = mockProps({ mode: 'out-in' }, true /*withKeepAlive*/) + const root = nodeOps.createElement('div') + const App = { + render() { + return h(BaseTransition, props, () => { + return h( + KeepAlive, + { include: includeRef.value }, + toggle.value ? h(trueComp) : h('div'), + ) + }) + }, + } + render(h(App), root) + + // trigger toggle + toggle.value = false + includeRef.value = [] + + await nextTick() + expect(unmountSpy).toHaveBeenCalledTimes(1) + }) }) // #6835 diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index db6088cf5c6..7697096bcd7 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -254,7 +254,7 @@ const KeepAliveImpl: ComponentOptions = { pendingCacheKey = null if (!slots.default) { - return null + return (current = null) } const children = slots.default()