Skip to content

Commit acfe1ad

Browse files
committed
warn using reserved attrs as prop (close vuejs#4241)
1 parent e4fd632 commit acfe1ad

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/core/instance/state.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
isPlainObject,
1919
bind,
2020
validateProp,
21-
noop
21+
noop,
22+
makeMap
2223
} from '../util/index'
2324

2425
export function initState (vm: Component) {
@@ -30,6 +31,8 @@ export function initState (vm: Component) {
3031
initWatch(vm)
3132
}
3233

34+
const isReservedProp = makeMap('key,ref,slot')
35+
3336
function initProps (vm: Component) {
3437
const props = vm.$options.props
3538
if (props) {
@@ -42,6 +45,12 @@ function initProps (vm: Component) {
4245
const key = keys[i]
4346
/* istanbul ignore else */
4447
if (process.env.NODE_ENV !== 'production') {
48+
if (isReservedProp(key)) {
49+
warn(
50+
`"${key}" is a reserved attribute and cannot be used as component prop.`,
51+
vm
52+
)
53+
}
4554
defineReactive(vm, key, validateProp(key, props, propsData, vm), () => {
4655
if (vm.$parent && !observerState.isSettingProps) {
4756
warn(

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,13 @@ describe('Options props', () => {
441441
expect(spy.calls.count()).toBe(2)
442442
}).then(done)
443443
})
444+
445+
it('warn reserved props', () => {
446+
new Vue({
447+
props: {
448+
key: String
449+
}
450+
})
451+
expect(`"key" is a reserved attribute`).toHaveBeenWarned()
452+
})
444453
})

0 commit comments

Comments
 (0)