mirror of
https://github.com/vuejs/vue.git
synced 2024-11-21 20:28:54 +00:00
polish: improve invalid method warning with type info (#8974)
close #8017
This commit is contained in:
parent
a7658e03a1
commit
613cb52bf3
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user