Skip to content

Commit

Permalink
fix: fix <keep-alive> include/exclude logic for anonymous components
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 20, 2017
1 parent b278120 commit a23b913
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/core/components/keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
54 changes: 53 additions & 1 deletion test/unit/features/component/component-keep-alive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>`,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a23b913

Please sign in to comment.