new Vue发生了什么?
首先查看源码src/core/instance/index.js
import { initMixin } from './init'
import { stateMixin } from './state'
import { renderMixin } from './render'
import { eventsMixin } from './events'
import { lifecycleMixin } from './lifecycle'
import { warn } from '../util/index'
function Vue (options) {
// 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)
}
//初始化方法混入
initMixin(Vue)
stateMixin(Vue)
eventsMixin(Vue)
lifecycleMixin(Vue)
renderMixin(Vue)
export default Vue
_init
从上述代码中看到_init,查找 src/core/instance/init.js
//混入方法入参Vue
export function initMixin (Vue: Class<Component>) {
// Vue构造函数中的this._init 增加原型链_init
Vue.prototype._init = function (options?: Object) {
//上下文转义到vm中
const vm: Component = this
// a uid
vm._uid = uid++
let startTag, endTag
//开始标记结束标记
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
startTag = `vue-perf-start:${vm._uid}`
endTag = `vue-perf-end:${vm._uid}`
mark(startTag)
}
// a flag to avoid this being observed
vm._isVue = true
// 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 {
vm.$options = mergeOptions(
//合并 解析的构造函数内的参数 并挂载到vm.$options上
resolveConstructorOptions(vm.constructor),
options || {},
vm
)
}
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
//初始化vm
initProxy(vm)
} else {
vm._renderProxy = vm
}
// expose real self 暴露vm
vm._self = vm
//初始化生命周期函数
initLifecycle(vm)
//初始化自定义事件
initEvents(vm)
//初始化渲染
initRender(vm)
//执行回调起beforeCreate生命周期
callHook(vm, 'beforeCreate')
//初始化state即data/props之前初始化注入inject
initInjections(vm) // resolve injections before data/props
//初始化state/props的数据双向绑定
initState(vm)
// //初始化state即data/props之后初始化provide
initProvide(vm) // resolve provide after data/props
//调用created生命周期钩子
callHook(vm, 'created')
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
vm._name = formatComponentName(vm, false)
mark(endTag)
measure(`vue ${vm._name} init`, startTag, endTag)
}
//之后挂载dom元素
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
}
}
总结
从上述中我们看到new Vue(options) 具体做了如下事情:
- 执行构造函数
- 上下文转义到vm
- 如果options && options._isComponent 为true,则开始初始化内部组件实例,否则合并配置解析的构造函数内的参数 并挂载到vm.$options上
- 初始化生命周期、初始化事件、初始化渲染
- 回调beforeCreate 生命周期
- 初始化注入inject 、初始化state、初始化provide
- 调用created 生命周期
- 挂载dom元素
initInternalComponent 方法和resolveConstructorOptions介绍
function initInternalComponent (vm: Component, options: InternalComponentOptions) {
const opts = vm.$options = Object.create(vm.constructor.options)
// doing this because it's faster than dynamic enumeration.
//父级参数
opts.parent = options.parent
//传入绑定数据v-bind
opts.propsData = options.propsData
//父级节点
opts._parentVnode = options._parentVnode
//父级监听@v-on
opts._parentListeners = options._parentListeners
//渲染子级
opts._renderChildren = options._renderChildren
//组件标签
opts._componentTag = options._componentTag
//父级元素
opts._parentElm = options._parentElm
//涉及元素,编号元素
opts._refElm = options._refElm
//渲染
if (options.render) {
opts.render = options.render
opts.staticRenderFns = options.staticRenderFns
}
}
//解析Vue构造函数内的参数
export function resolveConstructorOptions (Ctor: Class<Component>) {
let options = Ctor.options
//有super属性,说明Ctor是Vue.extend构建的子类
if (Ctor.super) {
const superOptions = resolveConstructorOptions(Ctor.super)
const cachedSuperOptions = Ctor.superOptions Vue构造函数上的options,如directives,filters
//顶级参数和缓存参数不同
if (superOptions !== cachedSuperOptions) {
// super option changed, 顶级参数已经更改
// need to resolve new options.
//解析顶级参数
Ctor.superOptions = superOptions
//检查是否有任何延迟修改/附加 参数(#4976)
// 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
}
}
}
return options
}