diff --git a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts index 6d3f6a9b8b6..5aa3b80284b 100644 --- a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts +++ b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts @@ -539,6 +539,16 @@ describe('KeepAlive', () => { await nextTick() assertHookCalls(one, [2, 2, 1, 1, 1]) assertHookCalls(two, [1, 1, 1, 1, 0]) + + includeRef.value = '' + await nextTick() + assertHookCalls(one, [2, 2, 1, 1, 1]) + assertHookCalls(two, [1, 1, 1, 1, 1]) + + viewRef.value = 'two' + await nextTick() + assertHookCalls(one, [2, 2, 1, 1, 2]) + assertHookCalls(two, [2, 2, 1, 1, 1]) }) test('on exclude change', async () => { @@ -558,6 +568,16 @@ describe('KeepAlive', () => { await nextTick() assertHookCalls(one, [2, 2, 1, 1, 1]) assertHookCalls(two, [1, 1, 1, 1, 0]) + + excludeRef.value = '' + await nextTick() + assertHookCalls(one, [2, 2, 1, 1, 1]) + assertHookCalls(two, [1, 1, 1, 1, 0]) + + excludeRef.value = 'two' + await nextTick() + assertHookCalls(one, [2, 2, 1, 1, 1]) + assertHookCalls(two, [1, 1, 1, 1, 1]) }) test('on include change + view switch', async () => { diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index a2c8c2bf1a7..349d74bbb62 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -220,8 +220,8 @@ const KeepAliveImpl: ComponentOptions = { watch( () => [props.include, props.exclude], ([include, exclude]) => { - include && pruneCache(name => matches(include, name)) - exclude && pruneCache(name => !matches(exclude, name)) + include != null && pruneCache(name => matches(include, name)) + exclude != null && pruneCache(name => !matches(exclude, name)) }, // prune post-render after `current` has been updated { flush: 'post', deep: true }, @@ -300,8 +300,8 @@ const KeepAliveImpl: ComponentOptions = { const { include, exclude, max } = props if ( - (include && (!name || !matches(include, name))) || - (exclude && name && matches(exclude, name)) + (include != null && (!name || !matches(include, name))) || + (exclude != null && name && matches(exclude, name)) ) { current = vnode return rawVNode