Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wanshenggang committed Nov 1, 2017
1 parent c355319 commit cb94ec7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core/components/keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export default {
const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
if (componentOptions) {
// check pattern
const name: ?string = getComponentName(componentOptions)
if (name && (
const name: ?string = componentOptions && componentOptions.Ctor.options.name
if (!name || (
(this.exclude && matches(this.exclude, name)) ||
(this.include && !matches(this.include, name))
)) {
Expand Down
51 changes: 51 additions & 0 deletions test/unit/features/component/component-keep-alive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('Component keep-alive', () => {
let components, one, two, el
beforeEach(() => {
one = {
name: 'one',
template: '<div>one</div>',
created: jasmine.createSpy('one created'),
mounted: jasmine.createSpy('one mounted'),
Expand All @@ -16,6 +17,7 @@ describe('Component keep-alive', () => {
destroyed: jasmine.createSpy('one destroyed')
}
two = {
name: 'two',
template: '<div>two</div>',
created: jasmine.createSpy('two created'),
mounted: jasmine.createSpy('two mounted'),
Expand Down Expand Up @@ -477,6 +479,52 @@ describe('Component keep-alive', () => {
}).then(done)
})

// #6938
it('should not cache anonymous component', done => {
const one = {
name: 'cache',
data () {
return {
random: Math.random(0, 10)
}
},
template: `<div class="child">{{ random }}</div>`
}
const two = {
data () {
return {
random: Math.random(0, 10)
}
},
template: `<div class="child">{{ random }}</div>`
}
const vm = new Vue({
data: { view: 'one' },
template: `
<div>
<keep-alive>
<component :is="view" ></component>
</keep-alive>
</div>
`,
components: { one, two }
}).$mount()

let cacheText, noCacheText
waitForUpdate(() => {
cacheText = vm.$el.textContent
vm.view = 'two'
}).then(() => {
noCacheText = vm.$el.textContent
expect(noCacheText).not.toBe(cacheText)
vm.view = 'one'
}).then(() => {
expect(vm.$el.textContent).toBe(cacheText)
vm.view = 'two'
}).then(() => {
expect(vm.$el.textContent).not.toBe(noCacheText)
}).then(done)
})
if (!isIE9) {
it('with transition-mode out-in', done => {
let next
Expand Down Expand Up @@ -977,16 +1025,19 @@ describe('Component keep-alive', () => {
},
components: {
aa: {
name: 'aa',
template: '<div>a</div>',
created: spyA,
destroyed: spyAD
},
bb: {
name: 'bb',
template: '<div>bbb</div>',
created: spyB,
destroyed: spyBD
},
cc: {
name: 'cc',
template: '<div>ccc</div>',
created: spyC,
destroyed: spyCD
Expand Down

0 comments on commit cb94ec7

Please sign in to comment.