Skip to content

Commit

Permalink
warn and handle missing get in computed (fix #5265) (#5267)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmc059000 authored and yyx990803 committed Mar 27, 2017
1 parent 3209f6f commit 6fcfdbd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/core/instance/state.js
Expand Up @@ -147,7 +147,16 @@ function initComputed (vm: Component, computed: Object) {

for (const key in computed) {
const userDef = computed[key]
const getter = typeof userDef === 'function' ? userDef : userDef.get
let getter = typeof userDef === 'function' ? userDef : userDef.get
if (process.env.NODE_ENV !== 'production') {
if (getter === undefined) {
warn(
`No getter function has been defined for computed property "${key}".`,
vm
)
getter = noop
}
}
// create internal watcher for the computed property.
watchers[key] = new Watcher(vm, getter, noop, computedWatcherOptions)

Expand Down
27 changes: 27 additions & 0 deletions test/unit/features/options/computed.spec.js
Expand Up @@ -48,6 +48,33 @@ describe('Options computed', () => {
}).then(done)
})

it('warn with setter and no getter', () => {
const vm = new Vue({
template: `
<div>
<test></test>
</div>
`,
components: {
test: {
data () {
return {
a: 1
}
},
computed: {
b: {
set (v) { this.a = v }
}
},
template: `<div>{{a}}</div>`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div>1</div>')
expect('No getter function has been defined for computed property "b".').toHaveBeenWarned()
})

it('watching computed', done => {
const spy = jasmine.createSpy('watch computed')
const vm = new Vue({
Expand Down

0 comments on commit 6fcfdbd

Please sign in to comment.