Skip to content

feat(state.js): check if method is not a function in the component de… #8974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,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
)
Expand All @@ -279,7 +279,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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/unit/features/options/methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down