源码分析
1. 片段一
文件: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'
// new vue()
function Vue (options) {
// !(this instanceof Vue) 只能通过new创建实例使用
if (process.env.NODE_ENV !== 'production' &&
!(this instanceof Vue)
) {
warn('Vue is a constructor and should be called with the `new` keyword')
}
// 构造函数仅执行了_init
this._init(options)
}
// 实现vue的实例方法和属性
initMixin(Vue) // 定义Vue原型上的init方法 Vue.prototype._init,实现一系列初始化过程
stateMixin(Vue) // 状态相关api $data,$props,$set,$delete,$watch
eventsMixin(Vue) // 事件相关api $on,$once,$off,$emit
lifecycleMixin(Vue) // 生命周期api _update,$forceUpdate,$destroy
renderMixin(Vue) // 渲染api _render,$nextTick
export default Vue
思路:
new Vue({..}) 判断是否通过new创建实例,而不是直接调用Vue()方法。然后执行_init()方法,实现一系列初始化过程。
2. 片段二
文件:vue\src\core\instance\init.js。进入this._init(options)方法文件
源码:
/* @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>) {
Vue.prototype._init = function (options?: Object) {
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 {
// this.$options 实例属性
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) // 初始化相关生命周期属性 $children、$root、$children、$refs
initEvents(vm) // 自定义事件监听
initRender(vm) // 插槽解析($slots)。 render(h)方法里的h:_c() 和 $createElement()
callHook(vm, 'beforeCreate') // 生命周期钩子: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')// 生命周期钩子: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)
}
// 如果设置了el选项,则自动调用$mount方法
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
}
}
思路:
具体的vue初始化:
- 合并配置(this.$options):将用户选项和系统默认选项合并
- initLifecycle(vm) 初始化相关生命周期属性 $children、$root、$children、$refs
- initEvents(vm) 自定义事件监听
- initRender(vm) 插槽解析($slots)。 render(h)方法里的h:_c() 和 $createElement()
- 初始化组件各种状态、响应式。initInjections(vm)、initState(vm)、initProvide(vm)
- 执行两个生命周期钩子。beforeCreate、created
- 执行 $mount 挂载方法
文件地址:github