function initProps (vm: Component, propsOptions: Object) {
`$options.propsData` 是在子组件创建 vnode 的过程中,将父组件传入的 props 保存为 propsData,
之后在子组件合并配置时,合并到 $options 中
const propsData = vm.$options.propsData || {}
最终所有的 props 都会保存进实例的 _props 中
const props = vm._props = {}
用于缓存
const keys = vm.$options._propKeys = []
取消 observer 观测
const isRoot = !vm.$parent
if (!isRoot) {
toggleObserving(false)
}
for (const key in propsOptions) {
keys.push(key)
校验类型并取值
const value = validateProp(key, propsOptions, propsData, vm)
将key 添加到实例的 _props 中
defineReactive(props, key, value, () => {
if (!isRoot && !isUpdatingChildComponent) {
warn(
`Avoid mutating a prop directly since the value will be ` +
`overwritten whenever the parent component re-renders. ` +
`Instead, use a data or computed property based on the prop's ` +
`value. Prop being mutated: "${key}"`,
vm
)
}
})
}
toggleObserving(true)
}
initProps
最新推荐文章于 2024-10-02 05:39:15 发布