From 6fcfdbd83f424eba19933d453420d762c9bad767 Mon Sep 17 00:00:00 2001 From: Kenneth Crawford Date: Sun, 26 Mar 2017 21:41:29 -0500 Subject: [PATCH] warn and handle missing get in computed (fix #5265) (#5267) --- src/core/instance/state.js | 11 ++++++++- test/unit/features/options/computed.spec.js | 27 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) 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({