Skip to content
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

fix #4041, warn overriding Vue's internal methods #4111

Merged
merged 2 commits into from
Nov 4, 2016
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
10 changes: 8 additions & 2 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
noop
} from '../util/index'

import BuiltinVue from '../index'

export function initState (vm: Component) {
vm._watchers = []
initProps(vm)
Expand Down Expand Up @@ -143,12 +145,16 @@ function initMethods (vm: Component) {
if (methods) {
for (const key in methods) {
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
if (process.env.NODE_ENV !== 'production' && methods[key] == null) {
warn(
if (process.env.NODE_ENV !== 'production') {
methods[key] == null && warn(
`method "${key}" has an undefined value in the component definition. ` +
`Did you reference the function correctly?`,
vm
)
hasOwn(BuiltinVue.prototype, key) && warn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just use vm instead of importing Vue here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I also considered that approach when coding this. But if we use vm or vm.constructor.prototype, it will break constructor style instance because $emit or so isn't their own properties.

`Avoid overriding Vue's internal method "${key}".`,
vm
)
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/features/options/methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ describe('Options methods', () => {
})
expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
})

it('should warn overriding builtin methods', () => {
new Vue({
methods: {
$emit () {
}
}
})
expect(`Avoid overriding Vue's internal method "$emit".`).toHaveBeenWarned()
})
})