diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 3441c60c83..bbe7f88298 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -172,18 +172,15 @@ function initComputed (vm: Component, computed: Object) { for (const key in computed) { const userDef = computed[key] - 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 - } + const getter = typeof userDef === 'function' ? userDef : userDef.get + if (process.env.NODE_ENV !== 'production' && getter == null) { + warn( + `Getter is missing for computed property "${key}".`, + vm + ) } // create internal watcher for the computed property. - watchers[key] = new Watcher(vm, getter, noop, computedWatcherOptions) + watchers[key] = new Watcher(vm, getter || noop, noop, computedWatcherOptions) // component-defined computed properties are already defined on the // component prototype. We only need to define computed properties defined @@ -214,6 +211,15 @@ export function defineComputed (target: any, key: string, userDef: Object | Func ? userDef.set : noop } + if (process.env.NODE_ENV !== 'production' && + sharedPropertyDefinition.set === noop) { + sharedPropertyDefinition.set = function () { + warn( + `Computed property "${key}" was assigned to but it has no setter.`, + this + ) + } + } Object.defineProperty(target, key, sharedPropertyDefinition) } diff --git a/test/unit/features/options/computed.spec.js b/test/unit/features/options/computed.spec.js index 7bd0b151ac..ae1c91e4e3 100644 --- a/test/unit/features/options/computed.spec.js +++ b/test/unit/features/options/computed.spec.js @@ -75,7 +75,19 @@ describe('Options computed', () => { } }).$mount() expect(vm.$el.innerHTML).toBe('
1
') - expect('No getter function has been defined for computed property "b".').toHaveBeenWarned() + expect('Getter is missing for computed property "b".').toHaveBeenWarned() + }) + + it('warn assigning to computed with no setter', () => { + const vm = new Vue({ + computed: { + b () { + return 1 + } + } + }) + vm.b = 2 + expect(`Computed property "b" was assigned to but it has no setter.`).toHaveBeenWarned() }) it('watching computed', done => {