Jelajahi Sumber

warn missing handler for child component v-on

Evan You 10 tahun lalu
induk
melakukan
244ea477b7
2 mengubah file dengan 25 tambahan dan 2 penghapusan
  1. 12 2
      src/instance/internal/events.js
  2. 13 0
      test/unit/specs/instance/events_spec.js

+ 12 - 2
src/instance/internal/events.js

@@ -38,8 +38,18 @@ export default function (Vue) {
       if (eventRE.test(name)) {
         name = name.replace(eventRE, '')
         handler = (vm._scope || vm._context).$eval(attrs[i].value, true)
-        handler._fromParent = true
-        vm.$on(name.replace(eventRE), handler)
+        if (typeof handler === 'function') {
+          handler._fromParent = true
+          vm.$on(name.replace(eventRE), handler)
+        } else if (process.env.NODE_ENV !== 'production') {
+          warn(
+            'v-on:' + name + '="' + attrs[i].value + '"' + (
+              vm.$options.name
+                ? ' on component <' + vm.$options.name + '>'
+                : ''
+            ) + ' expects a function value, got ' + handler
+          )
+        }
       }
     }
   }

+ 13 - 0
test/unit/specs/instance/events_spec.js

@@ -237,6 +237,19 @@ describe('Instance Events', function () {
       expect(vm.a).toBe(1)
     })
 
+    it('warn missing handler for child component v-on', function () {
+      new Vue({
+        el: document.createElement('div'),
+        template: '<comp @test="onThat"></comp>',
+        components: {
+          comp: {}
+        }
+      })
+      expect(hasWarned(
+        'v-on:test="onThat" on component <comp> expects a function value'
+      )).toBe(true)
+    })
+
     it('passing $arguments', function () {
       new Vue({
         el: document.createElement('div'),