Skip to content

Commit a23b913

Browse files
committed
fix: fix <keep-alive> include/exclude logic for anonymous components
1 parent b278120 commit a23b913

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/core/components/keep-alive.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ export default {
8787
if (componentOptions) {
8888
// check pattern
8989
const name: ?string = getComponentName(componentOptions)
90-
if (!name || (
91-
(this.exclude && matches(this.exclude, name)) ||
92-
(this.include && !matches(this.include, name))
93-
)) {
90+
const { include, exclude } = this
91+
if (
92+
// not included
93+
(include && (!name || !matches(include, name))) ||
94+
// excluded
95+
(exclude && name && matches(exclude, name))
96+
) {
9497
return vnode
9598
}
9699

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

+53-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ describe('Component keep-alive', () => {
551551
})
552552

553553
// #6938
554-
it('should not cache anonymous component', done => {
554+
it('should not cache anonymous component when include is specified', done => {
555555
const Foo = {
556556
name: 'foo',
557557
template: `<div>foo</div>`,
@@ -604,6 +604,58 @@ describe('Component keep-alive', () => {
604604
}).then(done)
605605
})
606606

607+
it('should cache anonymous components if include is not specified', done => {
608+
const Foo = {
609+
template: `<div>foo</div>`,
610+
created: jasmine.createSpy('foo')
611+
}
612+
613+
const Bar = {
614+
template: `<div>bar</div>`,
615+
created: jasmine.createSpy('bar')
616+
}
617+
618+
const Child = {
619+
functional: true,
620+
render (h, ctx) {
621+
return h(ctx.props.view ? Foo : Bar)
622+
}
623+
}
624+
625+
const vm = new Vue({
626+
template: `
627+
<keep-alive>
628+
<child :view="view"></child>
629+
</keep-alive>
630+
`,
631+
data: {
632+
view: true
633+
},
634+
components: { Child }
635+
}).$mount()
636+
637+
function assert (foo, bar) {
638+
expect(Foo.created.calls.count()).toBe(foo)
639+
expect(Bar.created.calls.count()).toBe(bar)
640+
}
641+
642+
expect(vm.$el.textContent).toBe('foo')
643+
assert(1, 0)
644+
vm.view = false
645+
waitForUpdate(() => {
646+
expect(vm.$el.textContent).toBe('bar')
647+
assert(1, 1)
648+
vm.view = true
649+
}).then(() => {
650+
expect(vm.$el.textContent).toBe('foo')
651+
assert(1, 1)
652+
vm.view = false
653+
}).then(() => {
654+
expect(vm.$el.textContent).toBe('bar')
655+
assert(1, 1)
656+
}).then(done)
657+
})
658+
607659
if (!isIE9) {
608660
it('with transition-mode out-in', done => {
609661
let next

0 commit comments

Comments
 (0)