From 3ad60fea73d042fc9a127d19de8329948d3f2ef0 Mon Sep 17 00:00:00 2001 From: wangzongxu <308929264@qq.com> Date: Fri, 16 Apr 2021 23:56:32 +0800 Subject: [PATCH] feat(warn): warn computed conflict with methods (#10119) Co-authored-by: wangzongxu --- src/core/instance/state.js | 2 ++ test/unit/features/options/computed.spec.js | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 67f9dae381d..db0d8acd087 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -202,6 +202,8 @@ function initComputed (vm: Component, computed: Object) { warn(`The computed property "${key}" is already defined in data.`, vm) } else if (vm.$options.props && key in vm.$options.props) { warn(`The computed property "${key}" is already defined as a prop.`, vm) + } else if (vm.$options.methods && key in vm.$options.methods) { + warn(`The computed property "${key}" is already defined as a method.`, vm) } } } diff --git a/test/unit/features/options/computed.spec.js b/test/unit/features/options/computed.spec.js index edc20bad3bf..17cd9516640 100644 --- a/test/unit/features/options/computed.spec.js +++ b/test/unit/features/options/computed.spec.js @@ -206,6 +206,18 @@ describe('Options computed', () => { expect(`computed property "a" is already defined as a prop`).toHaveBeenWarned() }) + it('warn conflict with methods', () => { + new Vue({ + computed: { + a: () => 2 + }, + methods: { + a: () => {} + } + }) + expect(`computed property "a" is already defined as a method`).toHaveBeenWarned() + }) + it('rethrow computed error', () => { const vm = new Vue({ computed: {