Skip to content

Commit 3bbe952

Browse files
fix vuejs#4041, warn overriding Vue's internal methods
1 parent 35f145c commit 3bbe952

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/core/instance/state.js

Lines changed: 9 additions & 2 deletions
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,17 @@ 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+
`You're overriding Vue's internal method "${key}". ` +
156+
`Beware of misbehaviors.`,
157+
vm
158+
)
152159
}
153160
}
154161
}

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

Lines changed: 10 additions & 0 deletions
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(`You're overriding Vue's internal method "$emit". Beware of misbehaviors.`).toHaveBeenWarned()
36+
})
2737
})

0 commit comments

Comments
 (0)