相信大家對vue的生命周期都很了解,但是對於每個生命周期都干了些什么呢?下面主要講在vue的beforeCreate 這個生命周期的時候都做了什么,官方說明進行了事件和什么周期的初始化。
其實實際代碼里主要做了一下四件事,在函數中 Vue.prototype._init 查看
1.處理該實例的初始化選項---vm.$options
2.初始化生命周期---initLifecycle
3.初始化事件---initEvents
4.初始化render函數---initRender
第一步分兩種情況進行處理: 一、是否是組件進行實例化,二、實例化非組件Vue對象
亮代碼,在 _init 函數中
Vue.prototype._init = function (options) { //省略部分代碼 // merge options if (options && options._isComponent) { // 如果是組件安裝組件的方式進行組件的vm.$options屬性初始化 // 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 { // 不是組件 將傳入的參數 options 與 構造函數上的屬性options (通過全局混入定義的)進行合並 ,合並 // 時根據是否有自定義合並策略進行合並Vue.config.optionMergeStrategies ,合並后賦值給當前實例的$options 屬性 vm.$options = mergeOptions( resolveConstructorOptions(vm.constructor), //返回構造函數的options屬性 options || {}, vm ); }