2
0
Эх сурвалжийг харах

polish: improve invalid method warning with type info (#8974)

close #8017
Alexey Tirman 7 жил өмнө
parent
commit
613cb52bf3

+ 3 - 3
src/core/instance/state.js

@@ -257,9 +257,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
         )
@@ -277,7 +277,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)
   }
 }
 

+ 3 - 3
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', () => {