Vue源碼學(xué)習(xí)之初始化模塊init.js解析
我們看到了VUE分了很多模塊(initMixin()stateMixin()eventsMixin()lifecycleMixin()renderMixin()),通過使用Mixin模式,都是使用了JavaScript原型繼承的原理,在Vue的原型上面增加屬性和方法。我們繼續(xù)跟著this._init(options)走,這個(gè)一點(diǎn)擊進(jìn)去就知道了是進(jìn)入了init.js文件是在initMixin函數(shù)里面給Vue原型添加的_init方法。首先來從宏觀看看這個(gè)init文件,可以看出主要是導(dǎo)出了兩個(gè)函數(shù):initMixin和resolveConstructorOptions,具體作用我們一步步來討論。咋的一看這個(gè)文件,可能有些童鞋會(huì)看不明白函數(shù)參數(shù)括號(hào)里面寫的是什么鬼,這個(gè)其實(shí)是應(yīng)用了flow的類型檢查,具體flow的使用這里就不介紹了,有興趣的請(qǐng)移步:https://flow.org/en/
我們現(xiàn)在來看第一個(gè)函數(shù)initMixin,Vue實(shí)例在初始化的時(shí)候就調(diào)用了這個(gè)函數(shù),
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 */ 【**注:istanbul 是代碼覆蓋率檢測(cè)工具,此注釋為代碼測(cè)試用**】
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
startTag = `vue-perf-init:${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(`${vm._name} init`, startTag, endTag)
}
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
}
}
我們本著宏觀簡(jiǎn)化原則,這個(gè)函數(shù)里面前面有三個(gè)if判斷工作我們可以先不細(xì)化討論,大致第一個(gè)是用performance做性能監(jiān)測(cè),第二個(gè)合并option,第三個(gè)是做代理攔截,是ES6新特性,可參考阮一峰大神關(guān)于proxy的介紹【http://es6.ruanyifeng.com/#docs/proxy】。那么就進(jìn)入了初始化函數(shù)主要點(diǎn):
initLifecycle(vm) //生命周期變量初始化
initEvents(vm) //事件監(jiān)聽初始化
initRender(vm) //初始化渲染
callHook(vm, 'beforeCreate') //回調(diào)鉤子beforeCreate
initInjections(vm) //初始化注入
initState(vm) // prop/data/computed/method/watch狀態(tài)初始化
initProvide(vm) // resolve provide after data/props
callHook(vm, 'created') //回調(diào)鉤子created
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
vm._name = formatComponentName(vm, false)
mark(endTag)
measure(`${vm._name} init`, startTag, endTag)
}
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
這里來一個(gè)插曲start
V2.1.8及以前的版本】這里比較方便理解在生命周期created之后再做render,那么在created之前就無法獲取DOM。這也是在有些源碼解析文章里面很容易見到的分析,也是正確的
initLifecycle(vm) initEvents(vm) callHook(vm, 'beforeCreate') initState(vm) callHook(vm, 'created') initRender(vm)
v2.1.9及以后的版本】但到這里一開始就懵逼了很久render提到beforeCreate之前去了,那豈不是DOM在beforeCreate之前就能獲取到了?顯然不對(duì)了,請(qǐng)注意render雖然提前了,但是后面多了一個(gè)if這個(gè)if里面才獲取DOM的關(guān)鍵,這個(gè)if在2.1.8版本之前是在render函數(shù)里面的,在2.1.9之后被提出來,然后render函數(shù)提前了,至于為何提前暫未了解,此處只是踩了一個(gè)看其他源碼解析不同版本帶來的坑!
initLifecycle(vm)
initEvents(vm)
initRender(vm)
callHook(vm, 'beforeCreate')
initState(vm)
callHook(vm, 'created')
if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
插曲end,繼續(xù)
1.initLifecycle
function initLifecycle (vm: Component) {
const options = vm.$options
// locate first non-abstract parent
let parent = options.parent //我理解為父實(shí)例或者父組件
if (parent && !options.abstract) { //例子中沒有parent,斷點(diǎn)代碼的時(shí)候自動(dòng)跳過
while (parent.$options.abstract && parent.$parent) {
parent = parent.$parent
}
parent.$children.push(vm)
}
vm.$parent = parent
vm.$root = parent ? parent.$root : vm
vm.$children = []
vm.$refs = {}
vm._watcher = null
vm._inactive = null
vm._directInactive = false
vm._isMounted = false
vm._isDestroyed = false
vm._isBeingDestroyed = false
}
這個(gè)函數(shù)主要是有父實(shí)例的情況下處理vm.$parent和vm.$children這倆個(gè)實(shí)例屬性,我此處沒有就跳過,其他的就是新增了一些實(shí)例屬性
2.initEvents
function initEvents (vm: Component) {
vm._events = Object.create(null)
vm._hasHookEvent = false
// init parent attached events
const listeners = vm.$options._parentListeners
if (listeners) {
updateComponentListeners(vm, listeners)
}
}
又新增兩個(gè)屬性,后面那個(gè)if條件里面是有父組件的事件時(shí)初始化,估計(jì)就是props和events父子組件通信的事件內(nèi)容。
3.initRender
function initRender (vm: Component) {
vm._vnode = null // the root of the child tree
vm._staticTrees = null
const parentVnode = vm.$vnode = vm.$options._parentVnode
const renderContext = parentVnode && parentVnode.context
vm.$slots = resolveSlots(vm.$options._renderChildren, renderContext)
vm.$scopedSlots = emptyObject
vm._c = (a, b, c, d) => createElement(vm, a, b, c, d, false)
vm.$createElement = (a, b, c, d) => createElement(vm, a, b, c, d, true)
const parentData = parentVnode && parentVnode.data
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
defineReactive(vm, '$attrs', parentData && parentData.attrs, () => {
!isUpdatingChildComponent && warn(`$attrs is readonly.`, vm)
}, true)
defineReactive(vm, '$listeners', vm.$options._parentListeners, () => {
!isUpdatingChildComponent && warn(`$listeners is readonly.`, vm)
}, true)
} else {
defineReactive(vm, '$attrs', parentData && parentData.attrs, null, true)
defineReactive(vm, '$listeners', vm.$options._parentListeners, null, true)
}
}
此函數(shù)也是初始化了節(jié)點(diǎn)屬性信息,綁定createElement函數(shù)到實(shí)例【并未掛載】,接下來調(diào)用beforeCreate回調(diào)鉤子;——TODO1:后續(xù)專題分析VUE渲染邏輯
4.initInjections
function initInjections (vm: Component) {
const result = resolveInject(vm.$options.inject, vm)
if (result) {
observerState.shouldConvert = false
Object.keys(result).forEach(key => {
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
defineReactive(vm, key, result[key], () => {
warn(
`Avoid mutating an injected value directly since the changes will be ` +
`overwritten whenever the provided component re-renders. ` +
`injection being mutated: "${key}"`,
vm
)
})
} else {
defineReactive(vm, key, result[key])
}
})
observerState.shouldConvert = true
}
}
此函數(shù)也是當(dāng)有inject屬性時(shí)做處理,源碼例子無inject斷點(diǎn)跑暫時(shí)跳過
5.initState
function initState (vm: Component) {
vm._watchers = []
const opts = vm.$options
if (opts.props) initProps(vm, opts.props)
if (opts.methods) initMethods(vm, opts.methods)
if (opts.data) {
initData(vm)
} else {
observe(vm._data = {}, true /* asRootData */)
}
if (opts.computed) initComputed(vm, opts.computed)
if (opts.watch && opts.watch !== nativeWatch) {
initWatch(vm, opts.watch)
}
}
可以看出此處是對(duì)options傳入的props/methods/data/computed/watch屬性做初始化————TODO2:分析每個(gè)屬性的初始化
6.initProvide
function initProvide (vm: Component) {
const provide = vm.$options.provide
if (provide) {
vm._provided = typeof provide === 'function'
? provide.call(vm)
: provide
}
}
這個(gè)函數(shù)跟4.initInjections在同一個(gè)inject.js中,也是在傳入?yún)?shù)有provide屬性時(shí)做處理,暫時(shí)跳過,然后就到了created回調(diào)鉤子,最后的vm.$mount接入TODO1;
今天initMixin到此結(jié)束,以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js中用v-bind綁定class的注意事項(xiàng)
關(guān)于數(shù)據(jù)綁定一個(gè)常見需求就是操作元素的class列表和它的內(nèi)聯(lián)樣式。因?yàn)樗鼈兌际菍傩?,我們可以?v-bind 處理它們,但是使用v-bind綁定class的時(shí)候我們要有一些注意事項(xiàng),下面這篇文章就給大家分享了下要注意的方面,希望能對(duì)大家有所幫助,下面來一起看看吧。2016-12-12
vue關(guān)于select組件綁定的值為數(shù)字類型的問題
這篇文章主要介紹了vue關(guān)于select組件綁定的值為數(shù)字類型的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue.js 2.0和Cordova開發(fā)webApp環(huán)境搭建方法
下面小編就為大家分享一篇Vue.js 2.0和Cordova開發(fā)webApp環(huán)境搭建方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
vue-cli3項(xiàng)目生產(chǎn)和測(cè)試環(huán)境分包后文件名和數(shù)量不一致解決
這篇文章主要為大家介紹了vue-cli3項(xiàng)目生產(chǎn)和測(cè)試環(huán)境分包后文件名和數(shù)量不一致解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Vue格式化數(shù)據(jù)后切換頁面出現(xiàn)NaN問題及解決
這篇文章主要介紹了Vue格式化數(shù)據(jù)后切換頁面出現(xiàn)NaN問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
對(duì) Vue-Router 進(jìn)行單元測(cè)試的方法
這篇文章主要介紹了對(duì) Vue-Router 進(jìn)行單元測(cè)試的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
vue使用自定義事件的表單輸入組件用法詳解【日期組件與貨幣組件】
這篇文章主要介紹了vue使用自定義事件的表單輸入組件用法,結(jié)合實(shí)例形式詳細(xì)分析了vue.js日期組件與貨幣組件相關(guān)操作技巧及注意事項(xiàng),需要的朋友可以參考下2020-06-06

