Skip to content

Commit 4078ce9

Browse files
HerringtonDarkholmeyyx990803
authored andcommitted
fix #4041, warn overriding Vue's internal methods (#4111)
* fix #4041, warn overriding Vue's internal methods * prefer concise warning message
1 parent 35f145c commit 4078ce9

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/core/instance/state.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
noop
2222
} from '../util/index'
2323

24+
import BuiltinVue from '../index'
25+
2426
export function initState (vm: Component) {
2527
vm._watchers = []
2628
initProps(vm)
@@ -143,12 +145,16 @@ function initMethods (vm: Component) {
143145
if (methods) {
144146
for (const key in methods) {
145147
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
146-
if (process.env.NODE_ENV !== 'production' && methods[key] == null) {
147-
warn(
148+
if (process.env.NODE_ENV !== 'production') {
149+
methods[key] == null && warn(
148150
`method "${key}" has an undefined value in the component definition. ` +
149151
`Did you reference the function correctly?`,
150152
vm
151153
)
154+
hasOwn(BuiltinVue.prototype, key) && warn(
155+
`Avoid overriding Vue's internal method "${key}".`,
156+
vm
157+
)
152158
}
153159
}
154160
}

test/unit/features/options/methods.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ describe('Options methods', () => {
2424
})
2525
expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
2626
})
27+
28+
it('should warn overriding builtin methods', () => {
29+
new Vue({
30+
methods: {
31+
$emit () {
32+
}
33+
}
34+
})
35+
expect(`Avoid overriding Vue's internal method "$emit".`).toHaveBeenWarned()
36+
})
2737
})

0 commit comments

Comments
 (0)