Skip to content

Commit f8429fe

Browse files
yyx990803hefeng
authored and
hefeng
committed
chore: warn methods that conflict with internals
close vuejs#6312
1 parent 2077685 commit f8429fe

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function initData (vm: Component) {
137137
if (process.env.NODE_ENV !== 'production') {
138138
if (methods && hasOwn(methods, key)) {
139139
warn(
140-
`method "${key}" has already been defined as a data property.`,
140+
`Method "${key}" has already been defined as a data property.`,
141141
vm
142142
)
143143
}
@@ -260,22 +260,28 @@ function initMethods (vm: Component, methods: Object) {
260260
process.env.NODE_ENV !== 'production' && checkOptionType(vm, 'methods')
261261
const props = vm.$options.props
262262
for (const key in methods) {
263-
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
264263
if (process.env.NODE_ENV !== 'production') {
265264
if (methods[key] == null) {
266265
warn(
267-
`method "${key}" has an undefined value in the component definition. ` +
266+
`Method "${key}" has an undefined value in the component definition. ` +
268267
`Did you reference the function correctly?`,
269268
vm
270269
)
271270
}
272271
if (props && hasOwn(props, key)) {
273272
warn(
274-
`method "${key}" has already been defined as a prop.`,
273+
`Method "${key}" has already been defined as a prop.`,
275274
vm
276275
)
277276
}
277+
if ((key in vm) && isReserved(key)) {
278+
warn(
279+
`Method "${key}" conflicts with an existing Vue instance method. ` +
280+
`Avoid defining component methods that start with _ or $.`
281+
)
282+
}
278283
}
284+
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
279285
}
280286
}
281287

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Options methods', () => {
2525
hello: undefined
2626
}
2727
})
28-
expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
28+
expect(`Method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
2929
})
3030

3131
it('should warn methods conflicting with data', () => {
@@ -37,6 +37,15 @@ describe('Options methods', () => {
3737
foo () {}
3838
}
3939
})
40-
expect(`method "foo" has already been defined as a data property`).toHaveBeenWarned()
40+
expect(`Method "foo" has already been defined as a data property`).toHaveBeenWarned()
41+
})
42+
43+
it('should warn methods conflicting with internal methods', () => {
44+
new Vue({
45+
methods: {
46+
_update () {}
47+
}
48+
})
49+
expect(`Method "_update" conflicts with an existing Vue instance method`).toHaveBeenWarned()
4150
})
4251
})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ describe('Options props', () => {
333333
}
334334
}
335335
}).$mount()
336-
expect(`method "a" has already been defined as a prop`).toHaveBeenWarned()
336+
expect(`Method "a" has already been defined as a prop`).toHaveBeenWarned()
337337
expect(`Avoid mutating a prop directly`).toHaveBeenWarned()
338338
})
339339

0 commit comments

Comments
 (0)