Skip to content

Commit 2deb0c7

Browse files
authored
fix(keep-alive): handle "0" as cache key (#1622)
fix #1621
1 parent 8facaef commit 2deb0c7

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

packages/runtime-core/src/components/KeepAlive.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ const KeepAliveImpl = {
182182
// cache sub tree in beforeMount/Update (i.e. right after the render)
183183
let pendingCacheKey: CacheKey | null = null
184184
const cacheSubtree = () => {
185-
if (pendingCacheKey) {
185+
// fix #1621, the pendingCacheKey could be 0
186+
if (pendingCacheKey != null) {
186187
cache.set(pendingCacheKey, instance.subTree)
187188
}
188189
}

packages/vue/__tests__/index.spec.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createApp } from '../src'
1+
import { createApp, ref, nextTick } from '../src'
22
import { mockWarn } from '@vue/shared'
33

44
describe('compiler + runtime integration', () => {
@@ -18,6 +18,62 @@ describe('compiler + runtime integration', () => {
1818
expect(container.innerHTML).toBe(`0`)
1919
})
2020

21+
it('keep-alive with compiler + runtime integration', async () => {
22+
const container = document.createElement('div')
23+
const one = {
24+
name: 'one',
25+
template: 'one',
26+
created: jest.fn(),
27+
mounted: jest.fn(),
28+
activated: jest.fn(),
29+
deactivated: jest.fn(),
30+
destroyed: jest.fn()
31+
}
32+
33+
const toggle = ref(true)
34+
35+
const App = {
36+
template: `
37+
<keep-alive>
38+
<one v-if="toggle"></one>
39+
</keep-alive>
40+
`,
41+
data() {
42+
return {
43+
toggle
44+
}
45+
},
46+
components: {
47+
One: one
48+
}
49+
}
50+
createApp(App).mount(container)
51+
expect(container.innerHTML).toBe(`one`)
52+
expect(one.created).toHaveBeenCalledTimes(1)
53+
expect(one.mounted).toHaveBeenCalledTimes(1)
54+
expect(one.activated).toHaveBeenCalledTimes(1)
55+
expect(one.deactivated).toHaveBeenCalledTimes(0)
56+
expect(one.destroyed).toHaveBeenCalledTimes(0)
57+
58+
toggle.value = false;
59+
await nextTick()
60+
expect(container.innerHTML).toBe(`<!--v-if-->`)
61+
expect(one.created).toHaveBeenCalledTimes(1)
62+
expect(one.mounted).toHaveBeenCalledTimes(1)
63+
expect(one.activated).toHaveBeenCalledTimes(1)
64+
expect(one.deactivated).toHaveBeenCalledTimes(1)
65+
expect(one.destroyed).toHaveBeenCalledTimes(0)
66+
67+
toggle.value = true;
68+
await nextTick()
69+
expect(container.innerHTML).toBe(`one`)
70+
expect(one.created).toHaveBeenCalledTimes(1)
71+
expect(one.mounted).toHaveBeenCalledTimes(1)
72+
expect(one.activated).toHaveBeenCalledTimes(2)
73+
expect(one.deactivated).toHaveBeenCalledTimes(1)
74+
expect(one.destroyed).toHaveBeenCalledTimes(0)
75+
})
76+
2177
it('should support runtime template via CSS ID selector', () => {
2278
const container = document.createElement('div')
2379
const template = document.createElement('div')

0 commit comments

Comments
 (0)