1、_init方法
执行了new Vue()之后,实际上是通过调用_init方法触发一系列的初始化流程。而_init方法是通过initMixin方法挂载到Vue原型中的:
export function initMixin (Vue: Class<Component>) {
Vue.prototype._init = function (options?: Object) {
//do something
}
}
其中“Vue: Class”、“options?: Object”的表达是flow类型检查。
Vue.prototype._init = function (options?: Object) {
//vm => this的别名
const vm: Component = this
//给vm一个唯一的uid
// a uid
vm._uid = uid++
let startTag, endTag
/* istanbul ignore if */
//用于测试性能,可了解下window.performance
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
//内部组件的options._isComponent是true,mian.js里面new Vue()时为false
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选项
//resolveConstructorOptions函数是获取当前实例中的构造函数的options选项以及它所有的父级的构造函数的options
vm.$options = mergeOptions(
resolveConstructorOptions(vm.constructor),
options || {
},
vm
)
}
/* istanbul ignore else */
//为Vue实例的_renderProxy属性赋值
if (process.env.NODE_ENV !== 'production') {
initProxy(vm)
} else {
vm._renderProxy = vm
}
// expose real self
vm._self = vm
initLifecycle(vm)
initEvents(vm)
initRender(vm)
callHook(vm, 'beforeCreate')
initInjections(vm) // resolve injections before data/props
initState(vm)
initProvide(vm) // resolve provide after data/props
callHook(vm, 'created')
/* istanbul ignore if */
//用于测试性能
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
vm._name = formatComponentName(vm, false)
mark(endTag)