-
data
浩销、provide
等選項(xiàng)使用mergeDataOrFn
-
生命周期鉤子 及
watch
合并為 數(shù)組蚣常,使得父子選項(xiàng)中的鉤子函數(shù)都能夠被執(zhí)行 -
directives
谤民、filters
及components
等資源選項(xiàng)值依,父子選項(xiàng)將以原型鏈的形式被處理苦丁,這樣才能夠在任何地方都是用內(nèi)置組件、指令等 -
props
镊逝、methods
壮啊、inject
、computed
等選項(xiàng)蹋半,父選項(xiàng)始終可用他巨,但是子選項(xiàng)會(huì)覆蓋同名的父選項(xiàng)字段 - 以上沒(méi)有提到的選項(xiàng)都使用默認(rèn)
defaultStrat
充坑,該選項(xiàng)策略是:只要子選項(xiàng)不是undefined
就是用子選項(xiàng)减江,否則使用父選項(xiàng)
附上 mergeDataOrFn
的實(shí)現(xiàn):
export function mergeDataOrFn (
parentVal: any,
childVal: any,
vm?: Component
): ?Function {
if (!vm) {
// in a Vue.extend merge, both should be functions
if (!childVal) {
return parentVal
}
if (!parentVal) {
return childVal
}
// when parentVal & childVal are both present,
// we need to return a function that returns the
// merged result of both functions... no need to
// check if parentVal is a function here because
// it has to be a function to pass previous merges.
return function mergedDataFn () {
return mergeData(
typeof childVal === 'function' ? childVal.call(this, this) : childVal,
typeof parentVal === 'function' ? parentVal.call(this, this) : parentVal
)
}
} else {
return function mergedInstanceDataFn () {
// instance merge
const instanceData = typeof childVal === 'function'
? childVal.call(vm, vm)
: childVal
const defaultData = typeof parentVal === 'function'
? parentVal.call(vm, vm)
: parentVal
if (instanceData) {
return mergeData(instanceData, defaultData)
} else {
return defaultData
}
}
}
}