要了解new Vue(),发生了什么其实就是要找到vue 是在哪里定义的:
src/core/index.js
import Vue from './instance/index'
// 1.初始化全局API:component/filter/directive/use/mixin/util/extend
initGlobalAPI(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'
// 1.声明构造函数
function Vue (options) {
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)
}
// 2.实例属性,实例方法:
initMixin(Vue) // _init()
stateMixin(Vue) // $data/$props/$set()/$delete()/$watch()
eventsMixin(Vue) // $emit()/$on/$off()/$once()
lifecycleMixin(Vue) // _update()/$forceUpdate()/$destroy()
renderMixin(Vue) // $nextTick()/_render()
export default Vue
/* @flow */
import config from '../config'
import { initProxy } from './proxy'
import { initState } from './state'
import { initRender } from './render'
import { initEvents } from './events'
import { mark, measure } from '../util/perf'
import { initLifecycle, callHook } from './lifecycle'
import { initProvide, initInjections } from './inject'
import { extend, mergeOptions, formatComponentName } from '../util/index'
let uid = 0
export function initMixin (Vue: Class<Component>) {
// 实现_init初始化方法
Vue.prototype._init = function (options?: Object) {
const vm: Component = this
// a uid
vm._uid = uid++
// a flag to avoid this being observed
vm._isVue = true
// 1.合并选项
// merge options
if (options && options._isComponent) {
initInternalComponent(vm, options)
} else {
vm.$options = mergeOptions(
resolveConstructorOptions(vm.constructor),
options || {},
vm
)
}
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
initProxy(vm)
} else {
vm._renderProxy = vm
}
// expose real self
// 初始化核心逻辑
vm._self = vm
initLifecycle(vm) // $parent/$root/...
initEvents(vm) // 自定义事件监听
initRender(vm) // $slots/$createElement
callHook(vm, 'beforeCreate')
initInjections(vm) // resolve injections before data/props
initState(vm) // props/methods/data/computed/watch
initProvide(vm) // resolve provide after data/props
callHook(vm, 'created')
// 当设置了el选项时,自动调用$mount
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
}
}
看到这里其实vue的生命周期图也就理解了
new(),依次实现以下:
- 初始化全局API:
component/filter/directive/use/mixin/util/extend
- 实例属性,实例方法:
initMixin(Vue) // _init()
stateMixin(Vue) // $data/$props/$set()/$delete()/$watch()
eventsMixin(Vue) // $emit()/$on/$off()/$once()
lifecycleMixin(Vue) // _update()/$forceUpdate()/$destroy()
renderMixin(Vue) // $nextTick()/_render()
- _init核心逻辑
// 初始化核心逻辑
vm._self = vm
initLifecycle(vm) // $parent/$root/...
initEvents(vm) // 自定义事件监听
initRender(vm) // $slots/$createElement
callHook(vm, 'beforeCreate')
initInjections(vm) // resolve injections before data/props
initState(vm) // props/methods/data/computed/watch
initProvide(vm) // resolve provide after data/props
callHook(vm, 'created')
// 当设置了el选项时,自动调用$mount
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}