fix: fix <keep-alive> include/exclude logic for anonymous components

This commit is contained in:
Evan You 2017-11-20 10:04:06 -05:00
parent b2781205b0
commit a23b913796
2 changed files with 60 additions and 5 deletions

View File

@ -87,10 +87,13 @@ export default {
if (componentOptions) {
// check pattern
const name: ?string = getComponentName(componentOptions)
if (!name || (
(this.exclude && matches(this.exclude, name)) ||
(this.include && !matches(this.include, name))
)) {
const { include, exclude } = this
if (
// not included
(include && (!name || !matches(include, name))) ||
// excluded
(exclude && name && matches(exclude, name))
) {
return vnode
}

View File

@ -551,7 +551,7 @@ describe('Component keep-alive', () => {
})
// #6938
it('should not cache anonymous component', done => {
it('should not cache anonymous component when include is specified', done => {
const Foo = {
name: 'foo',
template: `<div>foo</div>`,
@ -604,6 +604,58 @@ describe('Component keep-alive', () => {
}).then(done)
})
it('should cache anonymous components if include is not specified', done => {
const Foo = {
template: `<div>foo</div>`,
created: jasmine.createSpy('foo')
}
const Bar = {
template: `<div>bar</div>`,
created: jasmine.createSpy('bar')
}
const Child = {
functional: true,
render (h, ctx) {
return h(ctx.props.view ? Foo : Bar)
}
}
const vm = new Vue({
template: `
<keep-alive>
<child :view="view"></child>
</keep-alive>
`,
data: {
view: true
},
components: { Child }
}).$mount()
function assert (foo, bar) {
expect(Foo.created.calls.count()).toBe(foo)
expect(Bar.created.calls.count()).toBe(bar)
}
expect(vm.$el.textContent).toBe('foo')
assert(1, 0)
vm.view = false
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('bar')
assert(1, 1)
vm.view = true
}).then(() => {
expect(vm.$el.textContent).toBe('foo')
assert(1, 1)
vm.view = false
}).then(() => {
expect(vm.$el.textContent).toBe('bar')
assert(1, 1)
}).then(done)
})
if (!isIE9) {
it('with transition-mode out-in', done => {
let next