暴露出Vue src/core/instance/index.js
首先定义了名为Vue的函数
1
2
3
4
5
6
7
8
9function Vue (options) {
// 使用函数调用Vue()来调用Vue时给出错误警告
if (process.env.NODE_ENV !== 'production' &&
!(this instanceof Vue)
) {
warn('Vue is a constructor and should be called with the `new` keyword')
}
this._init(options)
}然后初始化&挂载一些功能
1
2
3
4
5initMixin(Vue)
stateMixin(Vue)
eventsMixin(Vue)
lifecycleMixin(Vue)
renderMixin(Vue)最后将Vue函数导出
先看initMixin(Vue) src/core/instance/init.js
initMixin在Vue.prototype上挂载了_init方法,该_init方法会在new Vue({})时首先调用。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16// 参数的处理
// merge options
if (options && options._isComponent) {
// optimize internal component instantiation
// since dynamic options merging is pretty slow, and none of the
// internal component options needs special treatment.
initInternalComponent(vm, options)
} else {
// 合并配置options
vm.$options = mergeOptions(
// 解析构造函数配置
resolveConstructorOptions(vm.constructor),
options || {},
vm
)
}
resolveConstructorOptions: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// 处理Vue与Vue子类这两种情况的options
export function resolveConstructorOptions (Ctor: Class<Component>) {
let options = Ctor.options
// 是否是Vue的子类
if (Ctor.super) {
const superOptions = resolveConstructorOptions(Ctor.super) // 找到超类的Options
const cachedSuperOptions = Ctor.superOptions //
if (superOptions !== cachedSuperOptions) { // 对比父类中的options 有没有发生变化
// super(Vue)的Options配置若改变,处理新的Options
// super option changed,
// need to resolve new options.
Ctor.superOptions = superOptions
// check if there are any late-modified/attached options (#4976)
const modifiedOptions = resolveModifiedOptions(Ctor)
// update base extend options
if (modifiedOptions) {
extend(Ctor.extendOptions, modifiedOptions)
}
options = Ctor.options = mergeOptions(superOptions, Ctor.extendOptions)
if (options.name) {
options.components[options.name] = Ctor
}
}
}
// 返回获merge自己的options与父类的options属性
return options
}
后续还有
1 | // 😀初始化生命周期 |
1 | // 😀️初始化事件 |
1 |
|
1 |
|
1 |
|
1 |
|
1 | // 在data/props之后初始化provide |
1 | // 😀触发created钩子 |