Skip to content

Commit d0eb48d

Browse files
author
zhuxinyong
committed
no message
1 parent c7b6950 commit d0eb48d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

2.6.0/src/core/instance/state.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ export function proxy (target: Object, sourceKey: string, key: string) {
4848
export function initState (vm: Component) {
4949
vm._watchers = []
5050
const opts = vm.$options
51+
// 先初始化 props,后初始化 data,这样就可以在 data 中使用 props 的数据
52+
// 同时也可以检测 data 中是否有和 props 、methods 重复的属性
5153
if (opts.props) initProps(vm, opts.props)
5254
if (opts.methods) initMethods(vm, opts.methods)
55+
5356
if (opts.data) {
5457
initData(vm)
5558
} else {
@@ -129,20 +132,24 @@ function initData (vm: Component) {
129132
let i = keys.length
130133
while (i--) {
131134
const key = keys[i]
135+
// 判断 methods 、props 中是否有重复的属性
132136
if (process.env.NODE_ENV !== 'production') {
137+
// 若和 methods 相同只在非生产环境给提示,但依旧会代理属性
133138
if (methods && hasOwn(methods, key)) {
134139
warn(
135140
`Method "${key}" has already been defined as a data property.`,
136141
vm
137142
)
138143
}
139144
}
145+
// 若和 props 重复则不代理,非生产环境还给出提示
140146
if (props && hasOwn(props, key)) {
141147
process.env.NODE_ENV !== 'production' && warn(
142148
`The data property "${key}" is already declared as a prop. ` +
143149
`Use prop default value instead.`,
144150
vm
145151
)
152+
// 不能是以 $ 和 _ 开头
146153
} else if (!isReserved(key)) {
147154
proxy(vm, `_data`, key)
148155
}

2.6.0/src/core/util/options.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,20 @@ function normalizeProps (options: Object, vm: ?Component) {
316316
val = props[key]
317317
name = camelize(key)
318318
res[name] = isPlainObject(val)
319+
// {
320+
// prop:{
321+
// type:String,
322+
// required:true,
323+
// }
324+
// }
319325
? val
326+
// {
327+
// prop:String
328+
// }
329+
//
330+
// {
331+
// prop:[String,Number]
332+
// }
320333
: { type: val }
321334
}
322335
} else if (process.env.NODE_ENV !== 'production') {

0 commit comments

Comments
 (0)