From 1bb7cba0e01485f25637c617cbc8b34a9021bb53 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 11 Oct 2016 17:32:35 -0400 Subject: [PATCH] improve event warning with component information (close #3831) --- src/core/instance/events.js | 2 +- src/core/instance/state.js | 11 +++++++---- src/core/vdom/helpers.js | 6 ++++-- src/platforms/web/runtime/modules/events.js | 2 +- test/unit/features/directives/on.spec.js | 2 +- test/unit/features/options/methods.spec.js | 2 +- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/core/instance/events.js b/src/core/instance/events.js index c98197b5..5c664d44 100644 --- a/src/core/instance/events.js +++ b/src/core/instance/events.js @@ -10,7 +10,7 @@ export function initEvents (vm: Component) { const on = bind(vm.$on, vm) const off = bind(vm.$off, vm) vm._updateListeners = (listeners, oldListeners) => { - updateListeners(listeners, oldListeners || {}, on, off) + updateListeners(listeners, oldListeners || {}, on, off, vm) } if (listeners) { vm._updateListeners(listeners) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 5d16ef43..36a8d3fb 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -142,10 +142,13 @@ function initMethods (vm: Component) { const methods = vm.$options.methods if (methods) { for (const key in methods) { - if (methods[key] != null) { - vm[key] = bind(methods[key], vm) - } else if (process.env.NODE_ENV !== 'production') { - warn(`Method "${key}" is undefined in options.`, vm) + vm[key] = methods[key] == null ? noop : bind(methods[key], vm) + 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 + ) } } } diff --git a/src/core/vdom/helpers.js b/src/core/vdom/helpers.js index d3a280b0..c2bfd6af 100644 --- a/src/core/vdom/helpers.js +++ b/src/core/vdom/helpers.js @@ -85,7 +85,8 @@ export function updateListeners ( on: Object, oldOn: Object, add: Function, - remove: Function + remove: Function, + vm: Component ) { let name, cur, old, fn, event, capture for (name in on) { @@ -93,7 +94,8 @@ export function updateListeners ( old = oldOn[name] if (!cur) { process.env.NODE_ENV !== 'production' && warn( - `Handler for event "${name}" is undefined.` + `Invalid handler for event "${name}": got ` + String(cur), + vm ) } else if (!old) { capture = name.charAt(0) === '!' diff --git a/src/platforms/web/runtime/modules/events.js b/src/platforms/web/runtime/modules/events.js index f08fa219..40c15530 100644 --- a/src/platforms/web/runtime/modules/events.js +++ b/src/platforms/web/runtime/modules/events.js @@ -15,7 +15,7 @@ function updateDOMListeners (oldVnode, vnode) { const remove = vnode.elm._v_remove || (vnode.elm._v_remove = (event, handler) => { vnode.elm.removeEventListener(event, handler) }) - updateListeners(on, oldOn, add, remove) + updateListeners(on, oldOn, add, remove, vnode.context) } export default { diff --git a/test/unit/features/directives/on.spec.js b/test/unit/features/directives/on.spec.js index f0e215ca..301c8ec0 100644 --- a/test/unit/features/directives/on.spec.js +++ b/test/unit/features/directives/on.spec.js @@ -230,7 +230,7 @@ describe('Directive v-on', () => { data: { none: null }, template: `
` }) - expect(`Handler for event "click" is undefined`).toHaveBeenWarned() + expect(`Invalid handler for event "click": got null`).toHaveBeenWarned() expect(() => { triggerEvent(vm.$el, 'click') }).not.toThrow() diff --git a/test/unit/features/options/methods.spec.js b/test/unit/features/options/methods.spec.js index b02b5f95..3e699114 100644 --- a/test/unit/features/options/methods.spec.js +++ b/test/unit/features/options/methods.spec.js @@ -22,6 +22,6 @@ describe('Options methods', () => { hello: undefined } }) - expect(`Method "hello" is undefined in options`).toHaveBeenWarned() + expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned() }) })