Skip to content

fix:#6938 <keep-alive> should not cache anonymous components #6985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@dsonet dsonet Nov 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you change the way of getting name? Plus doesn't need componentOptions && since it's already in the if branch.
IMO it should keep original logic opts.Ctor.options.name || opts.tag even if you want an optimization here which may not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review , already modified.

docs descripition:
The match is first checked on the component’s own name option, then its local registration name (the key in the parent’s components option) if the name option is not available. Anonymous components cannot be matched against.

Anonymous components always have a registered name( tag in code) , i think the descripition of docs may cause logical hole.

Copy link
Member

@jkzing jkzing Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cuteyumiko IMO we may need to keep opts.tag. For example:

const foo = {
  template: `<div>foo</div>`
}

new Vue({
  el: '#app',
  components: {
    foo
  }
})

In above case, you'll find Ctor.options doesn't have a name property, but there actually is tag in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ehhh..i see
i have done another test for caching anonymous components , it turns out caching logic is ok

#6938 it's problem may be casued by vue-router , i tracked this issue in a wrong way

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