Skip to content

Commit 6fcfdbd

Browse files
kmc059000yyx990803
authored andcommitted
warn and handle missing get in computed (fix vuejs#5265) (vuejs#5267)
1 parent 3209f6f commit 6fcfdbd

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/core/instance/state.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,16 @@ function initComputed (vm: Component, computed: Object) {
147147

148148
for (const key in computed) {
149149
const userDef = computed[key]
150-
const getter = typeof userDef === 'function' ? userDef : userDef.get
150+
let getter = typeof userDef === 'function' ? userDef : userDef.get
151+
if (process.env.NODE_ENV !== 'production') {
152+
if (getter === undefined) {
153+
warn(
154+
`No getter function has been defined for computed property "${key}".`,
155+
vm
156+
)
157+
getter = noop
158+
}
159+
}
151160
// create internal watcher for the computed property.
152161
watchers[key] = new Watcher(vm, getter, noop, computedWatcherOptions)
153162

test/unit/features/options/computed.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ describe('Options computed', () => {
4848
}).then(done)
4949
})
5050

51+
it('warn with setter and no getter', () => {
52+
const vm = new Vue({
53+
template: `
54+
<div>
55+
<test></test>
56+
</div>
57+
`,
58+
components: {
59+
test: {
60+
data () {
61+
return {
62+
a: 1
63+
}
64+
},
65+
computed: {
66+
b: {
67+
set (v) { this.a = v }
68+
}
69+
},
70+
template: `<div>{{a}}</div>`
71+
}
72+
}
73+
}).$mount()
74+
expect(vm.$el.innerHTML).toBe('<div>1</div>')
75+
expect('No getter function has been defined for computed property "b".').toHaveBeenWarned()
76+
})
77+
5178
it('watching computed', done => {
5279
const spy = jasmine.createSpy('watch computed')
5380
const vm = new Vue({

0 commit comments

Comments
 (0)