@@ -37,17 +37,18 @@ export function initState (vm: Component) {
37
37
38
38
const isReservedProp = { key : 1 , ref : 1 , slot : 1 }
39
39
40
- function initProps ( vm : Component , props : Object ) {
40
+ function initProps ( vm : Component , propsOptions : Object ) {
41
41
const propsData = vm . $options . propsData || { }
42
- vm . $props = { }
42
+ const props = vm . _props = { }
43
43
// cache prop keys so that future props updates can iterate using Array
44
44
// instead of dyanmic object key enumeration.
45
45
const keys = vm . $options . _propKeys = [ ]
46
46
const isRoot = ! vm . $parent
47
47
// root instance props should be converted
48
48
observerState . shouldConvert = isRoot
49
- for ( const key in props ) {
49
+ for ( const key in propsOptions ) {
50
50
keys . push ( key )
51
+ const value = validateProp ( key , propsOptions , propsData , vm )
51
52
/* istanbul ignore else */
52
53
if ( process . env . NODE_ENV !== 'production' ) {
53
54
if ( isReservedProp [ key ] ) {
@@ -56,7 +57,7 @@ function initProps (vm: Component, props: Object) {
56
57
vm
57
58
)
58
59
}
59
- defineReactive ( vm . $ props, key , validateProp ( key , props , propsData , vm ) , ( ) => {
60
+ defineReactive ( props , key , value , ( ) => {
60
61
if ( vm . $parent && ! observerState . isSettingProps ) {
61
62
warn (
62
63
`Avoid mutating a prop directly since the value will be ` +
@@ -68,9 +69,9 @@ function initProps (vm: Component, props: Object) {
68
69
}
69
70
} )
70
71
} else {
71
- defineReactive ( vm . $ props, key , validateProp ( key , props , propsData , vm ) )
72
+ defineReactive ( props , key , value )
72
73
}
73
- proxy ( vm , '$ props' , key )
74
+ proxy ( vm , props , key )
74
75
}
75
76
observerState . shouldConvert = true
76
77
}
@@ -100,7 +101,7 @@ function initData (vm: Component) {
100
101
vm
101
102
)
102
103
} else if ( ! isReserved ( keys [ i ] ) ) {
103
- proxy ( vm , '_data' , keys [ i ] )
104
+ proxy ( vm , data , keys [ i ] )
104
105
}
105
106
}
106
107
// observe data
@@ -200,9 +201,9 @@ export function stateMixin (Vue: Class<Component>) {
200
201
// when using Object.defineProperty, so we have to procedurally build up
201
202
// the object here.
202
203
const dataDef = { }
203
- dataDef . get = function ( ) {
204
- return this . _data
205
- }
204
+ dataDef . get = function ( ) { return this . _data }
205
+ const propsDef = { }
206
+ propsDef . get = function ( ) { return this . _props }
206
207
if ( process . env . NODE_ENV !== 'production' ) {
207
208
dataDef . set = function ( newData : Object ) {
208
209
warn (
@@ -211,8 +212,12 @@ export function stateMixin (Vue: Class<Component>) {
211
212
this
212
213
)
213
214
}
215
+ propsDef . set = function ( ) {
216
+ warn ( `$props is readonly.` , this )
217
+ }
214
218
}
215
219
Object . defineProperty ( Vue . prototype , '$data' , dataDef )
220
+ Object . defineProperty ( Vue . prototype , '$props' , propsDef )
216
221
217
222
Vue . prototype . $set = set
218
223
Vue . prototype . $delete = del
@@ -235,15 +240,15 @@ export function stateMixin (Vue: Class<Component>) {
235
240
}
236
241
}
237
242
238
- function proxy ( vm : Component , proxyName : '$props' | '_data' , key : string ) {
243
+ function proxy ( vm : Component , source : Object , key : string ) {
239
244
Object . defineProperty ( vm , key , {
240
245
configurable : true ,
241
246
enumerable : true ,
242
247
get : function proxyGetter ( ) {
243
- return vm [ proxyName ] [ key ]
248
+ return source [ key ]
244
249
} ,
245
250
set : function proxySetter ( val ) {
246
- vm [ proxyName ] [ key ] = val
251
+ source [ key ] = val
247
252
}
248
253
} )
249
254
}
0 commit comments