assets
strats
对象添加如下3个方法:
components
directives
filters
// Assets
function mergeAssets (
parentVal: ?Object,
childVal: ?Object,
vm?: Component,
key: string
): Object {
const res = Object.create(parentVal || null)
if (childVal) {
process.env.NODE_ENV !== 'production' && assertObjectType(key, childVal, vm)
return extend(res, childVal)
} else {
return res
}
}
分析:
以
parentVal
为原型创建一个res
对象如果
childVal
存在,将childVal
对象合并到res
对象上,同名key覆盖,不同命赋值,并返回合并后的res
对象如果
childVal
不存在,直接返回res
对象
遍历ASSET_TYPES
,为每个资源添加mergeAssets
方法
// ASSET_TYPES = ['component','directive','filter']
ASSET_TYPES.forEach(function (type) {
strats[type + 's'] = mergeAssets
})
assertObjectType
函数的作用是检测一直变量是否是纯对象
function assertObjectType (name: string, value: any, vm: ?Component) {
if (!isPlainObject(value)) {
warn(
`Invalid value for option "${name}": expected an Object, ` +
`but got ${toRawType(value)}.`,
vm
)
}
}
Last updated
Was this helpful?