Skip to content

Commit 3932a45

Browse files
committed
fix(keep-alive): should not destroy active instance when pruning cache
fix #7105
1 parent f93c158 commit 3932a45

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/core/components/keep-alive.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function pruneCacheEntry (
4141
current?: VNode
4242
) {
4343
const cached = cache[key]
44-
if (cached && cached !== current) {
44+
if (cached && (!current || cached.tag !== current.tag)) {
4545
cached.componentInstance.$destroy()
4646
}
4747
cache[key] = null

test/unit/features/component/component-keep-alive.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,34 @@ describe('Component keep-alive', () => {
656656
}).then(done)
657657
})
658658

659+
// #7105
660+
it('should not destroy active instance when pruning cache', done => {
661+
const Foo = {
662+
template: `<div>foo</div>`,
663+
destroyed: jasmine.createSpy('destroyed')
664+
}
665+
const vm = new Vue({
666+
template: `
667+
<div>
668+
<keep-alive :include="include">
669+
<foo/>
670+
</keep-alive>
671+
</div>
672+
`,
673+
data: {
674+
include: ['foo']
675+
},
676+
components: { Foo }
677+
}).$mount()
678+
// condition: a render where a previous component is reused
679+
vm.include = ['foo']
680+
waitForUpdate(() => {
681+
vm.include = ['']
682+
}).then(() => {
683+
expect(Foo.destroyed).not.toHaveBeenCalled()
684+
}).then(done)
685+
})
686+
659687
if (!isIE9) {
660688
it('with transition-mode out-in', done => {
661689
let next

0 commit comments

Comments
 (0)