Runtime + Compiler
我们来看带有编译功能的Vue版本,vue/src/platforms/web/entry-runtime-with-compiler.js
。
import config from 'core/config'
import { warn, cached } from 'core/util/index'
import { mark, measure } from 'core/util/perf'
import Vue from './runtime/index' // 引用了Vue
import { query } from './util/index'
import { compileToFunctions } from './compiler/index'
import { shouldDecodeNewlines, shouldDecodeNewlinesForHref } from './util/compat'
在这个文件中引用了./runtime/index
中的Vue
# vue/src/platforms/web/runtime/index
import Vue from 'core/index' // 引用了Vue
import config from 'core/config'
import { extend, noop } from 'shared/util'
import { mountComponent } from 'core/instance/lifecycle'
import { devtools, inBrowser } from 'core/util/index'
在./runtime/index
中又引用了core/index
中的Vue`
# vue/src/core/index
import Vue from './instance/index' // 引用了Vue
import { initGlobalAPI } from './global-api/index'
import { isServerRendering } from 'core/util/env'
import { FunctionalRenderContext } from 'core/vdom/create-functional-component'
initGlobalAPI(Vue)
Object.defineProperty(Vue.prototype, '$isServer', {
get: isServerRendering
})
Object.defineProperty(Vue.prototype, '$ssrContext', {
get () {
/* istanbul ignore next */
return this.$vnode && this.$vnode.ssrContext
}
})
// expose FunctionalRenderContext for ssr runtime helper installation
Object.defineProperty(Vue, 'FunctionalRenderContext', {
value: FunctionalRenderContext
})
Vue.version = '__VERSION__'
export default Vue
在这里又引用了./instance/index
中的Vue
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) {
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
终于,我们看到了Vue的庐山真面目,可以看到,Vue的底层实现是通过function
实现的类,并通过传入Vue来拓展Vue的其他模块,这种方式是class
方式难以做到的,因此,Vue的这种编程技巧也值得我们学习。
new Vue
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)
}
当我们通过new Vue 来初始化的时候,会调用_init
的私有方法,这个方法定义在vue/src/core/instance/init.js
当中。
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 {
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)
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)
measure(`vue ${vm._name} init`, startTag, endTag)
}
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
}
}
_init
方法是Vue原型上的方法,这个方法又是定义在initMixin
方法当中,initMixin
又在初始化Vue的时候被引入,这就能让Vue实例能调用_init
私有方法。
在_init
方法中,先做了三个if判断,这里我们不做细化讨论,大致第一个是用performance做性能监测,第二个合并option,第三个是做代理拦截。
然后我们就到了一系列的初始化工作
initLifecycle(vm) // 初始化生命周期
initEvents(vm) // 初始化事件
initRender(vm) // 初始化渲染
callHook(vm, 'beforeCreate') // 触发`beforeCreate`生命周期钩子
initInjections(vm) // resolve injections before data/props // 初始化注入
initState(vm) // 初始化状态 如prop/data/method/watch
initProvide(vm) // resolve provide after data/props
callHook(vm, 'created') // 触发`created`生命周期钩子
最终,我们通过传入option中el来实现Vue的挂载
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}