Просмотр исходного кода

warn missing event handlers (ref #3430)

Evan You 9 лет назад
Родитель
Сommit
6a156020ec
3 измененных файлов с 21 добавлено и 4 удалено
  1. 3 2
      src/core/instance/proxy.js
  2. 6 2
      src/core/vdom/helpers.js
  3. 12 0
      test/unit/features/directives/on.spec.js

+ 3 - 2
src/core/instance/proxy.js

@@ -22,8 +22,9 @@ if (process.env.NODE_ENV !== 'production') {
       const isAllowedGlobal = allowedGlobals(key)
       const isAllowedGlobal = allowedGlobals(key)
       if (!has && !isAllowedGlobal) {
       if (!has && !isAllowedGlobal) {
         warn(
         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
           target
         )
         )
       }
       }

+ 6 - 2
src/core/vdom/helpers.js

@@ -1,6 +1,6 @@
 /* @flow */
 /* @flow */
 
 
-import { isPrimitive } from '../util/index'
+import { isPrimitive, warn } from '../util/index'
 import VNode from './vnode'
 import VNode from './vnode'
 
 
 export function normalizeChildren (
 export function normalizeChildren (
@@ -82,7 +82,11 @@ export function updateListeners (
   for (name in on) {
   for (name in on) {
     cur = on[name]
     cur = on[name]
     old = oldOn[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) === '!'
       capture = name.charAt(0) === '!'
       event = capture ? name.slice(1) : name
       event = capture ? name.slice(1) : name
       if (Array.isArray(cur)) {
       if (Array.isArray(cur)) {

+ 12 - 0
test/unit/features/directives/on.spec.js

@@ -223,4 +223,16 @@ describe('Directive v-on', () => {
       expect(spy2.calls.count()).toBe(1)
       expect(spy2.calls.count()).toBe(1)
     }).then(done)
     }).then(done)
   })
   })
+
+  it('warn missing handlers', () => {
+    vm = new Vue({
+      el,
+      data: { none: null },
+      template: `<div @click="none"></div>`
+    })
+    expect(`Handler for event "click" is undefined`).toHaveBeenWarned()
+    expect(() => {
+      triggerEvent(vm.$el, 'click')
+    }).not.toThrow()
+  })
 })
 })