Vue.prototype._init = function (options?: Object) {
// 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')
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
}
initProxy
,作用域代理,攔截組件內訪問其它組件的數據。
initLifecycle
建立父子組件關系,在當前實例上添加一些屬性和生命周期標識。如: $children
、 $refs
、 _isMounted
等。
initEvents
用來存放除 @hook:生命周期鈎子名稱="綁定的函數"事件的對象。如: $on
、 $emit
等。
initRender
用於初始化 $slots
、 $attrs
、 $listeners
initInjections
初始化 inject
,一般用於更深層次的組件通信,相當於加強版子組件的 props
。用於組件庫開發較多。
initState
是很多選項初始化的匯總,包括: props
、methods
、data
、computed
和 watch
等。
initProvide
初始化 provide
vm.$mount
掛載實例。
