From d1bf5dab933ffa85f013201a65137548259f55c2 Mon Sep 17 00:00:00 2001 From: Aleksei Tirman Date: Sun, 21 Oct 2018 00:50:15 +0300 Subject: [PATCH] feat(state.js): check if method is not a function in the component definition warn about method is not a function in the development mode, pass noop if it is not a function fix #8017 --- src/core/instance/state.js | 6 +++--- test/unit/features/options/methods.spec.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index b1549b0dcc4..ad56de7e90d 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -259,9 +259,9 @@ function initMethods (vm: Component, methods: Object) { const props = vm.$options.props for (const key in methods) { if (process.env.NODE_ENV !== 'production') { - if (methods[key] == null) { + if (typeof methods[key] !== 'function') { warn( - `Method "${key}" has an undefined value in the component definition. ` + + `Method "${key}" has type "${typeof methods[key]}" in the component definition. ` + `Did you reference the function correctly?`, vm ) @@ -279,7 +279,7 @@ function initMethods (vm: Component, methods: Object) { ) } } - vm[key] = methods[key] == null ? noop : bind(methods[key], vm) + vm[key] = typeof methods[key] !== 'function' ? noop : bind(methods[key], vm) } } diff --git a/test/unit/features/options/methods.spec.js b/test/unit/features/options/methods.spec.js index 34f9ee88fc6..f1e5fc68b27 100644 --- a/test/unit/features/options/methods.spec.js +++ b/test/unit/features/options/methods.spec.js @@ -19,13 +19,13 @@ describe('Options methods', () => { expect(vm.a).toBe(2) }) - it('should warn undefined methods', () => { + it('should warn methods of not function type', () => { new Vue({ methods: { - hello: undefined + hello: {} } }) - expect(`Method "hello" has an undefined value in the component definition`).toHaveBeenWarned() + expect('Method "hello" has type "object" in the component definition').toHaveBeenWarned() }) it('should warn methods conflicting with data', () => {