From 3bbe9521fbf46bf1090d42eb448e0caa659eb4de Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 4 Nov 2016 13:29:07 +0800 Subject: [PATCH 1/2] fix #4041, warn overriding Vue's internal methods --- src/core/instance/state.js | 11 +++++++++-- test/unit/features/options/methods.spec.js | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 36a8d3fb11f..4e02a5743fb 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -21,6 +21,8 @@ import { noop } from '../util/index' +import BuiltinVue from '../index' + export function initState (vm: Component) { vm._watchers = [] initProps(vm) @@ -143,12 +145,17 @@ function initMethods (vm: Component) { if (methods) { for (const key in methods) { vm[key] = methods[key] == null ? noop : bind(methods[key], vm) - if (process.env.NODE_ENV !== 'production' && methods[key] == null) { - warn( + if (process.env.NODE_ENV !== 'production') { + methods[key] == null && warn( `method "${key}" has an undefined value in the component definition. ` + `Did you reference the function correctly?`, vm ) + hasOwn(BuiltinVue.prototype, key) && warn( + `You're overriding Vue's internal method "${key}". ` + + `Beware of misbehaviors.`, + vm + ) } } } diff --git a/test/unit/features/options/methods.spec.js b/test/unit/features/options/methods.spec.js index 3e6991144be..af13993125d 100644 --- a/test/unit/features/options/methods.spec.js +++ b/test/unit/features/options/methods.spec.js @@ -24,4 +24,14 @@ describe('Options methods', () => { }) expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned() }) + + it('should warn overriding builtin methods', () => { + new Vue({ + methods: { + $emit () { + } + } + }) + expect(`You're overriding Vue's internal method "$emit". Beware of misbehaviors.`).toHaveBeenWarned() + }) }) From 676dcb22601715d2f2835bb2c7ab18f8b7986f06 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 4 Nov 2016 19:35:32 +0800 Subject: [PATCH 2/2] prefer concise warning message --- src/core/instance/state.js | 3 +-- test/unit/features/options/methods.spec.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 4e02a5743fb..8469749349f 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -152,8 +152,7 @@ function initMethods (vm: Component) { vm ) hasOwn(BuiltinVue.prototype, key) && warn( - `You're overriding Vue's internal method "${key}". ` + - `Beware of misbehaviors.`, + `Avoid overriding Vue's internal method "${key}".`, vm ) } diff --git a/test/unit/features/options/methods.spec.js b/test/unit/features/options/methods.spec.js index af13993125d..d9c54a8590e 100644 --- a/test/unit/features/options/methods.spec.js +++ b/test/unit/features/options/methods.spec.js @@ -32,6 +32,6 @@ describe('Options methods', () => { } } }) - expect(`You're overriding Vue's internal method "$emit". Beware of misbehaviors.`).toHaveBeenWarned() + expect(`Avoid overriding Vue's internal method "$emit".`).toHaveBeenWarned() }) })