el和propsData

strats对象上添加两个相同的方法elpropsData,接收两个参数parentchild,返回一个其中的一个参数,有childVal返回childVal,否则就返回parentVal

/**
 * Options with restrictions
 */
if (process.env.NODE_ENV !== 'production') {
  strats.el = strats.propsData = function (parent, child, vm, key) {
    if (!vm) {
      warn(
        `option "${key}" can only be used during instance ` +
        'creation with the `new` keyword.'
      )
    }
    // 返回默认合并策略函数
    return defaultStrat(parent, child)
  }
}

通过上面的源码代码可以看出,合并策略对象strats添加两个相同的方法elpropsData,分别都接收两个参数parentchild,并且返回同一个默认合并策略函数defaultStrat

// Default strategy.
const defaultStrat = function (parentVal: any, childVal: any): any {
  return childVal === undefined
    ? parentVal
    : childVal
}

从上面代码可以看出,默认合并策略defaultStrat接收parentValchildVal两个参数,,返回两个参数中的某一个参数

规则是:有childVal返回childVal,否则就返回parentVal

Last updated