From 6a156020ec06dd9b74f6eff8602a321edd009927 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 14 Aug 2016 18:29:46 -0400 Subject: [PATCH] warn missing event handlers (ref #3430) --- src/core/instance/proxy.js | 5 +++-- src/core/vdom/helpers.js | 8 ++++++-- test/unit/features/directives/on.spec.js | 12 ++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/core/instance/proxy.js b/src/core/instance/proxy.js index 4d8186b8..e6a55db0 100644 --- a/src/core/instance/proxy.js +++ b/src/core/instance/proxy.js @@ -22,8 +22,9 @@ if (process.env.NODE_ENV !== 'production') { const isAllowedGlobal = allowedGlobals(key) if (!has && !isAllowedGlobal) { warn( - `Trying to access non-existent property "${key}" while rendering. ` + - `Make sure to declare reactive data properties in the data option.`, + `Property or method "${key}" is not defined on the instance but ` + + `referenced during render. Make sure to declare reactive data ` + + `properties in the data option.`, target ) } diff --git a/src/core/vdom/helpers.js b/src/core/vdom/helpers.js index 2b601362..77693ed7 100644 --- a/src/core/vdom/helpers.js +++ b/src/core/vdom/helpers.js @@ -1,6 +1,6 @@ /* @flow */ -import { isPrimitive } from '../util/index' +import { isPrimitive, warn } from '../util/index' import VNode from './vnode' export function normalizeChildren ( @@ -82,7 +82,11 @@ export function updateListeners ( for (name in on) { cur = on[name] old = oldOn[name] - if (!old) { + if (!cur) { + process.env.NODE_ENV !== 'production' && warn( + `Handler for event "${name}" is undefined.` + ) + } else if (!old) { capture = name.charAt(0) === '!' event = capture ? name.slice(1) : name if (Array.isArray(cur)) { diff --git a/test/unit/features/directives/on.spec.js b/test/unit/features/directives/on.spec.js index 4be5aae9..f0e215ca 100644 --- a/test/unit/features/directives/on.spec.js +++ b/test/unit/features/directives/on.spec.js @@ -223,4 +223,16 @@ describe('Directive v-on', () => { expect(spy2.calls.count()).toBe(1) }).then(done) }) + + it('warn missing handlers', () => { + vm = new Vue({ + el, + data: { none: null }, + template: `
` + }) + expect(`Handler for event "click" is undefined`).toHaveBeenWarned() + expect(() => { + triggerEvent(vm.$el, 'click') + }).not.toThrow() + }) })