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

close #8017
This commit is contained in:
Alexey Tirman 2018-12-01 02:06:24 +03:00 committed by Evan You
parent a7658e03a1
commit 613cb52bf3
2 changed files with 6 additions and 6 deletions

View File

@ -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)
}
}

View File

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