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 current node when the KeepAlive slots default is null #11787

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ const KeepAliveImpl: ComponentOptions = {
pendingCacheKey = null

if (!slots.default) {
current = null
return null
}

Expand Down
62 changes: 61 additions & 1 deletion packages/runtime-dom/__tests__/createApp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { createApp, h } from '../src'
import {
KeepAlive,
Transition,
createApp,
h,
nextTick,
onDeactivated,
onUnmounted,
ref,
} from '../src'

describe('createApp for dom', () => {
// #2926
Expand Down Expand Up @@ -39,4 +48,55 @@ describe('createApp for dom', () => {
// ensure no mutation happened to Root object
expect(originalObj).toMatchObject(Root)
})

test('Transition + KeepAlive', async () => {
const deactivated = vi.fn()
const unmounted = vi.fn()
const CompA = {
name: 'CompA',
setup() {
return () => h('h1', 'CompA')
},
}
const CompB = {
name: 'CompB',
setup() {
onDeactivated(deactivated)
onUnmounted(unmounted)
return () => h('h1', 'CompB')
},
}

const current = ref('CompA')
const cacheList = ref(['CompA', 'CompB'])
const App = createApp({
setup() {
return () =>
h(
Transition,
{ mode: 'out-in' },
{
default: () => [
h(
KeepAlive,
{
include: cacheList.value,
},
[current.value === 'CompA' ? h(CompA) : h(CompB)],
),
],
},
)
},
})
App.mount(document.createElement('div'))
current.value = 'CompB'
await new Promise(resolve => setTimeout(resolve, 500))
await nextTick()
cacheList.value.splice(1)
current.value = 'CompA'
await nextTick()
expect(deactivated).toHaveBeenCalledTimes(1)
expect(unmounted).toHaveBeenCalledTimes(1)
})
})