hooks
strats
对象上添加如下11
个钩子函数,并进行对应的hooks合并
beforeCreate
created
beforeMount
mounted
beforeUpdate
updated
beforeDestroy
destroyed
activated
deactivated
errorCaptured
// Hooks and props are merged as arrays.
function mergeHook (
parentVal: ?Array<Function>,
childVal: ?Function | ?Array<Function>
): ?Array<Function> {
// 根据三木运算符的右结合性,做如下区分
return childVal ? ( parentVal ? parentVal.concat(childVal) : ( Array.isArray(childVal) ? childVal : [childVal] )) : parentVal
// return childVal
// ? parentVal
// ? parentVal.concat(childVal)
// : Array.isArray(childVal)
// ? childVal
// : [childVal]
// : parentVal
}
分析:
如果
childVal
不存在,返回parentVal
(数组);否则,进入下一个判断,判断
parentVal
(数组)是否存在,如果存在,parentVal
拼接childVal
;否则,进入下一个判断, 判断
childVal
是否是数组,如果是,直接返回childVal
,否则,把childVal
变成数组。
遍历定义好的钩子常量,并对每个钩子常量添加mergeHook
方法进行hooks合并:
LIFECYCLE_HOOKS.forEach(hook => {
strats[hook] = mergeHook
})
Last updated
Was this helpful?