# Vue 源码中的方法
# validateComponentName
- 作用:校验传入的字符串是否合格
- 源码如下:
export 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.'
)
}
if (isBuiltInTag(name) || config.isReservedTag(name)) {
warn(
'Do not use built-in or reserved HTML elements as component ' +
'id: ' + name
)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# checkComponents
- 作用:校验局部注册的组件名是否符合规范
- 源码如下:
function checkComponents (options) {
for (const key in options.components) {
validateComponentName(key)
}
}
1
2
3
4
5
2
3
4
5
# mergeOptions
- 源码如下:
export function mergeOptions (parent,child,vm) {
if (process.env.NODE_ENV !== 'production') {
// 校验局部组件名是否合格
checkComponents(child)
}
// ?????
if (typeof child === 'function') {
child = child.options
}
// 格式化自定义组件中 prop 属性,转为对象的格式
normalizeProps(child, vm)
// 格式化自定义组件中的 inject 数据,转为对象的格式
normalizeInject(child, vm)
// 格式化局部自定义指令,转为对象的格式
normalizeDirectives(child)
// 处理 extends 和 mixins 的情况
if (!child._base) {
if (child.extends) {
parent = mergeOptions(parent, child.extends, vm)
}
if (child.mixins) {
for (let i = 0, l = child.mixins.length; i < l; i++) {
parent = mergeOptions(parent, child.mixins[i], vm)
}
}
}
// 定义一个新对象
const options = {}
let key
for (key in parent) {
mergeField(key)
}
for (key in child) {
if (!hasOwn(parent, key)) {
mergeField(key)
}
}
// 这个策略????
function mergeField (key) {
const strat = strats[key] || defaultStrat
options[key] = strat(parent[key], child[key], vm, key)
}
return options
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47