Skip to content

Commit 613cb52

Browse files
Stexxeyyx990803
authored andcommitted
polish: improve invalid method warning with type info (#8974)
close #8017
1 parent a7658e0 commit 613cb52

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

Diff for: src/core/instance/state.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ function initMethods (vm: Component, methods: Object) {
257257
const props = vm.$options.props
258258
for (const key in methods) {
259259
if (process.env.NODE_ENV !== 'production') {
260-
if (methods[key] == null) {
260+
if (typeof methods[key] !== 'function') {
261261
warn(
262-
`Method "${key}" has an undefined value in the component definition. ` +
262+
`Method "${key}" has type "${typeof methods[key]}" in the component definition. ` +
263263
`Did you reference the function correctly?`,
264264
vm
265265
)
@@ -277,7 +277,7 @@ function initMethods (vm: Component, methods: Object) {
277277
)
278278
}
279279
}
280-
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
280+
vm[key] = typeof methods[key] !== 'function' ? noop : bind(methods[key], vm)
281281
}
282282
}
283283

Diff for: test/unit/features/options/methods.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ describe('Options methods', () => {
1919
expect(vm.a).toBe(2)
2020
})
2121

22-
it('should warn undefined methods', () => {
22+
it('should warn methods of not function type', () => {
2323
new Vue({
2424
methods: {
25-
hello: undefined
25+
hello: {}
2626
}
2727
})
28-
expect(`Method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
28+
expect('Method "hello" has type "object" in the component definition').toHaveBeenWarned()
2929
})
3030

3131
it('should warn methods conflicting with data', () => {

0 commit comments

Comments
 (0)