Skip to content

Commit 3dc9338

Browse files
chriscasolayyx990803
authored andcommitted
Warn when defining a method with same name as a prop (#4950)
* Warn when defining a method with same name as a prop * update error message
1 parent 9ccffe7 commit 3dc9338

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/core/instance/state.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,23 @@ function createComputedGetter (key) {
183183
}
184184

185185
function initMethods (vm: Component, methods: Object) {
186+
const props = vm.$options.props
186187
for (const key in methods) {
187188
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
188-
if (process.env.NODE_ENV !== 'production' && methods[key] == null) {
189-
warn(
190-
`method "${key}" has an undefined value in the component definition. ` +
191-
`Did you reference the function correctly?`,
192-
vm
193-
)
189+
if (process.env.NODE_ENV !== 'production') {
190+
if (methods[key] == null) {
191+
warn(
192+
`method "${key}" has an undefined value in the component definition. ` +
193+
`Did you reference the function correctly?`,
194+
vm
195+
)
196+
}
197+
if (props && hasOwn(props, key)) {
198+
warn(
199+
`method "${key}" has already been defined as a prop.`,
200+
vm
201+
)
202+
}
194203
}
195204
}
196205
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,27 @@ describe('Options props', () => {
306306
expect('already declared as a prop').toHaveBeenWarned()
307307
})
308308

309+
it('should warn methods already defined as a prop', () => {
310+
new Vue({
311+
template: '<test a="1"></test>',
312+
components: {
313+
test: {
314+
template: '<div></div>',
315+
props: {
316+
a: null
317+
},
318+
methods: {
319+
a () {
320+
321+
}
322+
}
323+
}
324+
}
325+
}).$mount()
326+
expect(`method "a" has already been defined as a prop`).toHaveBeenWarned()
327+
expect(`Avoid mutating a prop directly`).toHaveBeenWarned()
328+
})
329+
309330
it('treat boolean props properly', () => {
310331
const vm = new Vue({
311332
template: '<comp ref="child" prop-a prop-b="prop-b"></comp>',

0 commit comments

Comments
 (0)