1.2 选项校验
选项合并过程我们更多的不可控在于不知道用户传了哪些配置选项,这些配置是否符合规范,所以每个选项的规范需要严格定义好,不允许用户按照规范外的标准来传递选项。因此在合并选项之前,很大的一部分工作是对选项的校验。其中components,prop,inject,directive
等都是检验的重点。下面只会列举components
和props
的校验讲解,其他的如inject, directive
校验类似,请自行对着源码解析。
function mergeOptions ( parent, child, vm ) {
{
checkComponents(child); // 合并前对选项components进行规范检测
}
if (typeof child === 'function') {
child = child.options;
}
normalizeProps(child, vm); // 校验props选项
normalizeInject(child, vm); // 校验inject选项
normalizeDirectives(child); // 校验directive选项
if (!child._base) {
if (child.extends) {
parent = mergeOptions(parent, child.extends, vm);
}
if (child.mixins) {
for (var i = 0, l = child.mixins.length; i < l; i++) {
parent = mergeOptions(parent, child.mixins[i], vm);
}
}
}
// 真正选项合并的代码
var options = {};
var key;
for (key in parent) {
mergeField(key);
}
for (key in child) {
if (!hasOwn(parent, key)) {
mergeField(key);
}
}
function mergeField (key) {
var strat = strats[key] || defaultStrat;
options[key] = strat(parent[key], child[key], vm, key);
}
return options
}
1.2.1 components规范检验
我们可以在vue
实例化时传入组件选项以此来注册组件。因此,组件命名需要遵守很多规范,比如组件名不能用html
保留的标签(如:img,p
),只能以字母开头等。因此在选项合并之前,需要对规范进行检查。
// components规范检查函数
function checkComponents (options) {
for (var key in options.components) {
validateComponentName(key);
}
}
function validateComponentName (name) {
if (!new RegExp(("^[a-zA-Z][\\-\\.0-9_" + (unicodeRegExp.source) + "]*$")).test(name)) {
// 正则判断检测是否为非法的标签
warn(
'Invalid component name: "' + name + '". Component names ' +
'should conform to valid custom element name in html5 specification.'
);
}
// 不能使用Vue自身自定义的组件名,如slot, component,不能使用html的保留标签,如 h1, svg等
if (isBuiltInTag(name) || config.isReservedTag(name)) {
warn(
'Do not use built-in or reserved HTML elements as component ' +
'id: ' + name
);
}
}
1.2.2 props规范检验
从vue
的使用文档看,props
选项的形式有两种,一种是['a', 'b', 'c']
的数组形式,一种是{ a: { type: 'String', default: 'hahah' }}
带有校验规则的形式。从源码上看,两种形式最终都会转换成对象的形式。
// props规范校验
function normalizeProps (options, vm) {
var props = options.props;
if (!props) { return }
var res = {};
var i, val, name;
// props选项数据有两种形式,一种是['a', 'b', 'c'],一种是{ a: { type: 'String', default: 'hahah' }}
if (Array.isArray(props)) {
i = props.length;
while (i--) {
val = props[i];
if (typeof val === 'string') {
name = camelize(val);
res[name] = { type: null }; // 默认将数组形式的props转换为对象形式。
} else {
// 保证是字符串
warn('props must be strings when using array syntax.');
}
}
} else if (isPlainObject(props)) {
for (var key in props) {
val = props[key];
name = camelize(key);
res[name] = isPlainObject(val)
? val
: { type: val };
}
} else {
// 非数组,非对象则判定props选项传递非法
warn(
"Invalid value for option \"props\": expected an Array or an Object, " +
"but got " + (toRawType(props)) + ".",
vm
);
}
options.props = res;
}
1.2.3 函数缓存
在读到props
规范检验时,我发现了一段函数优化的代码,他将每次执行函数后的值缓存起来,下次重复执行的时候调用缓存的数据,以此提高前端性能,这是典型的偏函数应用,可以参考我另一篇文章打造属于自己的underscore系列(五)- 偏函数和函数柯里化
function cached (fn) {
var cache = Object.create(null); // 创建空对象作为缓存对象
return (function cachedFn (str) {
var hit = cache[str];
return hit || (cache[str] = fn(str)) // 每次执行时缓存对象有值则不需要执行函数方法,没有则执行并缓存起来
})
}
var camelize = cached(function (str) {
// 将诸如 'a-b'的写法统一处理成驼峰写法'aB'
return str.replace(camelizeRE, function (_, c) { return c ? c.toUpperCase() : ''; })
});