From 4078ce9c58830fecff095ab96dc41693377794dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=28=C2=B4=E3=83=BB=CF=89=E3=83=BB=EF=BD=80=29?= Date: Fri, 4 Nov 2016 19:44:47 +0800 Subject: [PATCH] fix #4041, warn overriding Vue's internal methods (#4111) * fix #4041, warn overriding Vue's internal methods * prefer concise warning message --- src/core/instance/state.js | 10 ++++++++-- test/unit/features/options/methods.spec.js | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 36a8d3fb11f..8469749349f 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,16 @@ 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( + `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 3e6991144be..d9c54a8590e 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(`Avoid overriding Vue's internal method "$emit".`).toHaveBeenWarned() + }) })