Skip to content

Commit

Permalink
fix(keep-alive): add test case and modify code
Browse files Browse the repository at this point in the history
  • Loading branch information
张东 authored and 张东 committed Jul 18, 2024
1 parent 5901f25 commit 12afb29
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
10 changes: 4 additions & 6 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,8 @@ const KeepAliveImpl: ComponentOptions = {
watch(
() => [props.include, props.exclude],
([include, exclude]) => {
;(include || typeof include === 'string') &&
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 },
Expand Down Expand Up @@ -301,9 +300,8 @@ const KeepAliveImpl: ComponentOptions = {
const { include, exclude, max } = props

if (
((include || typeof include === 'string') &&
(!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
Expand Down

0 comments on commit 12afb29

Please sign in to comment.