diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 1a8d66a5b67..74977f852d3 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -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) diff --git a/test/unit/features/options/computed.spec.js b/test/unit/features/options/computed.spec.js index baed4a05418..9997c0b9251 100644 --- a/test/unit/features/options/computed.spec.js +++ b/test/unit/features/options/computed.spec.js @@ -48,6 +48,33 @@ describe('Options computed', () => { }).then(done) }) + it('warn with setter and no getter', () => { + const vm = new Vue({ + template: ` +
+ +
+ `, + components: { + test: { + data () { + return { + a: 1 + } + }, + computed: { + b: { + set (v) { this.a = v } + } + }, + template: `
{{a}}
` + } + } + }).$mount() + expect(vm.$el.innerHTML).toBe('
1
') + 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({