Skip to content

Commit 87ffd0d

Browse files
committed
tweaks
1 parent 5fea184 commit 87ffd0d

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

flow/component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ declare interface Component {
5353
_watcher: Watcher;
5454
_watchers: Array<Watcher>;
5555
_data: Object;
56+
_props: Object;
5657
_events: Object;
5758
_inactive: boolean;
5859
_isMounted: boolean;

src/core/instance/lifecycle.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,17 @@ export function lifecycleMixin (Vue: Class<Component>) {
143143
if (process.env.NODE_ENV !== 'production') {
144144
observerState.isSettingProps = true
145145
}
146+
const props = vm._props
146147
const propKeys = vm.$options._propKeys || []
147148
for (let i = 0; i < propKeys.length; i++) {
148149
const key = propKeys[i]
149-
vm[key] = validateProp(key, vm.$options.props, propsData, vm)
150+
props[key] = validateProp(key, vm.$options.props, propsData, vm)
150151
}
151152
observerState.shouldConvert = true
152153
if (process.env.NODE_ENV !== 'production') {
153154
observerState.isSettingProps = false
154155
}
156+
// keep a copy of raw propsData
155157
vm.$options.propsData = propsData
156158
}
157159
// update listeners

src/core/instance/state.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ export function initState (vm: Component) {
3737

3838
const isReservedProp = { key: 1, ref: 1, slot: 1 }
3939

40-
function initProps (vm: Component, props: Object) {
40+
function initProps (vm: Component, propsOptions: Object) {
4141
const propsData = vm.$options.propsData || {}
42-
vm.$props = {}
42+
const props = vm._props = {}
4343
// cache prop keys so that future props updates can iterate using Array
4444
// instead of dyanmic object key enumeration.
4545
const keys = vm.$options._propKeys = []
4646
const isRoot = !vm.$parent
4747
// root instance props should be converted
4848
observerState.shouldConvert = isRoot
49-
for (const key in props) {
49+
for (const key in propsOptions) {
5050
keys.push(key)
51+
const value = validateProp(key, propsOptions, propsData, vm)
5152
/* istanbul ignore else */
5253
if (process.env.NODE_ENV !== 'production') {
5354
if (isReservedProp[key]) {
@@ -56,7 +57,7 @@ function initProps (vm: Component, props: Object) {
5657
vm
5758
)
5859
}
59-
defineReactive(vm.$props, key, validateProp(key, props, propsData, vm), () => {
60+
defineReactive(props, key, value, () => {
6061
if (vm.$parent && !observerState.isSettingProps) {
6162
warn(
6263
`Avoid mutating a prop directly since the value will be ` +
@@ -68,9 +69,9 @@ function initProps (vm: Component, props: Object) {
6869
}
6970
})
7071
} else {
71-
defineReactive(vm.$props, key, validateProp(key, props, propsData, vm))
72+
defineReactive(props, key, value)
7273
}
73-
proxy(vm, '$props', key)
74+
proxy(vm, props, key)
7475
}
7576
observerState.shouldConvert = true
7677
}
@@ -100,7 +101,7 @@ function initData (vm: Component) {
100101
vm
101102
)
102103
} else if (!isReserved(keys[i])) {
103-
proxy(vm, '_data', keys[i])
104+
proxy(vm, data, keys[i])
104105
}
105106
}
106107
// observe data
@@ -200,9 +201,9 @@ export function stateMixin (Vue: Class<Component>) {
200201
// when using Object.defineProperty, so we have to procedurally build up
201202
// the object here.
202203
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 }
206207
if (process.env.NODE_ENV !== 'production') {
207208
dataDef.set = function (newData: Object) {
208209
warn(
@@ -211,8 +212,12 @@ export function stateMixin (Vue: Class<Component>) {
211212
this
212213
)
213214
}
215+
propsDef.set = function () {
216+
warn(`$props is readonly.`, this)
217+
}
214218
}
215219
Object.defineProperty(Vue.prototype, '$data', dataDef)
220+
Object.defineProperty(Vue.prototype, '$props', propsDef)
216221

217222
Vue.prototype.$set = set
218223
Vue.prototype.$delete = del
@@ -235,15 +240,15 @@ export function stateMixin (Vue: Class<Component>) {
235240
}
236241
}
237242

238-
function proxy (vm: Component, proxyName: '$props' | '_data', key: string) {
243+
function proxy (vm: Component, source: Object, key: string) {
239244
Object.defineProperty(vm, key, {
240245
configurable: true,
241246
enumerable: true,
242247
get: function proxyGetter () {
243-
return vm[proxyName][key]
248+
return source[key]
244249
},
245250
set: function proxySetter (val) {
246-
vm[proxyName][key] = val
251+
source[key] = val
247252
}
248253
})
249254
}

src/core/util/props.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ function getPropDefaultValue (vm: ?Component, prop: PropOptions, key: string): a
5454
}
5555
const def = prop.default
5656
// warn against non-factory defaults for Object & Array
57-
if (isObject(def)) {
58-
process.env.NODE_ENV !== 'production' && warn(
57+
if (process.env.NODE_ENV !== 'production' && isObject(def)) {
58+
warn(
5959
'Invalid default value for prop "' + key + '": ' +
6060
'Props with type Object/Array must use a factory function ' +
6161
'to return the default value.',
@@ -66,8 +66,8 @@ function getPropDefaultValue (vm: ?Component, prop: PropOptions, key: string): a
6666
// return previous default value to avoid unnecessary watcher trigger
6767
if (vm && vm.$options.propsData &&
6868
vm.$options.propsData[key] === undefined &&
69-
vm[key] !== undefined) {
70-
return vm[key]
69+
vm._props[key] !== undefined) {
70+
return vm._props[key]
7171
}
7272
// call factory function for non-Function types
7373
return typeof def === 'function' && prop.type !== Function

0 commit comments

Comments
 (0)