Skip to content

Commit 4d8226f

Browse files
committed
fix: keep-alive should not cache anonymous components
This only happens if the component is returned by a intermediate functional or abstract component, e.g. <router-view>. Fix #6938.
1 parent 6d6b373 commit 4d8226f

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/core/components/keep-alive.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default {
8787
if (componentOptions) {
8888
// check pattern
8989
const name: ?string = getComponentName(componentOptions)
90-
if (name && (
90+
if (!name || (
9191
(this.exclude && matches(this.exclude, name)) ||
9292
(this.include && !matches(this.include, name))
9393
)) {

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

+54
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,60 @@ describe('Component keep-alive', () => {
550550
expect(`Unknown custom element: <foo>`).toHaveBeenWarned()
551551
})
552552

553+
// #6938
554+
it('should not cache anonymous component', done => {
555+
const Foo = {
556+
name: 'foo',
557+
template: `<div>foo</div>`,
558+
created: jasmine.createSpy('foo')
559+
}
560+
561+
const Bar = {
562+
template: `<div>bar</div>`,
563+
created: jasmine.createSpy('bar')
564+
}
565+
566+
const Child = {
567+
functional: true,
568+
render (h, ctx) {
569+
return h(ctx.props.view ? Foo : Bar)
570+
}
571+
}
572+
573+
const vm = new Vue({
574+
template: `
575+
<keep-alive include="foo">
576+
<child :view="view"></child>
577+
</keep-alive>
578+
`,
579+
data: {
580+
view: true
581+
},
582+
components: { Child }
583+
}).$mount()
584+
585+
function assert (foo, bar) {
586+
expect(Foo.created.calls.count()).toBe(foo)
587+
expect(Bar.created.calls.count()).toBe(bar)
588+
}
589+
590+
expect(vm.$el.textContent).toBe('foo')
591+
assert(1, 0)
592+
vm.view = false
593+
waitForUpdate(() => {
594+
expect(vm.$el.textContent).toBe('bar')
595+
assert(1, 1)
596+
vm.view = true
597+
}).then(() => {
598+
expect(vm.$el.textContent).toBe('foo')
599+
assert(1, 1)
600+
vm.view = false
601+
}).then(() => {
602+
expect(vm.$el.textContent).toBe('bar')
603+
assert(1, 2)
604+
}).then(done)
605+
})
606+
553607
if (!isIE9) {
554608
it('with transition-mode out-in', done => {
555609
let next

0 commit comments

Comments
 (0)