diff --git a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts index 1e4176b6f..e4af150da 100644 --- a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts +++ b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts @@ -683,6 +683,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 () => { @@ -702,6 +712,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 f2b7bdf97..49cc5f6f4 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -226,8 +226,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 }, @@ -312,8 +312,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)) ) { // #11717 vnode.shapeFlag &= ~ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE