Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): reset keep alive flag when the component is removed from include #11718

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,4 +1121,57 @@ describe('KeepAlive', () => {
expect(mountedB).toHaveBeenCalledTimes(1)
expect(unmountedB).toHaveBeenCalledTimes(0)
})

// #11717
test('expect that the mounted, activated, and unmounted lifecycle hooks will be called the expected number of times.', async () => {
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
const About = {
name: 'About',
render() {
return h('h1', 'About')
},
}
const mountedHome = vi.fn()
const unmountedHome = vi.fn()
const activatedHome = vi.fn()
const deactivatedHome = vi.fn()
const Home = {
name: 'Home',
setup() {
onMounted(mountedHome)
onUnmounted(unmountedHome)
onDeactivated(deactivatedHome)
onActivated(activatedHome)
return () => {
h('h1', 'Home')
}
},
}
const activeViewName = ref('Home')
const cacheList = reactive(['Home'])
const App = createApp({
setup() {
return () => {
return [
h(
KeepAlive,
{
include: cacheList,
},
[activeViewName.value === 'Home' ? h(Home) : h(About)],
),
]
}
},
})
App.mount(nodeOps.createElement('div'))
await nextTick()
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
expect(mountedHome).toHaveBeenCalledTimes(1)
expect(activatedHome).toHaveBeenCalledTimes(1)
cacheList.splice(0, 1)
await nextTick()
activeViewName.value = 'About'
await nextTick()
expect(deactivatedHome).toHaveBeenCalledTimes(0)
expect(unmountedHome).toHaveBeenCalledTimes(1)
})
})
4 changes: 4 additions & 0 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ const KeepAliveImpl: ComponentOptions = {
(include && (!name || !matches(include, name))) ||
(exclude && name && matches(exclude, name))
) {
// #11717
if ((vnode.shapeFlag |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE)) {
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
vnode.shapeFlag &= ~ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
}
current = vnode
return rawVNode
}
Expand Down