Vue源碼學習(二)——從宏觀看Vue

上一篇文章咱們寫到從入口文件一步步找到Vue的構造函數,如今咱們要去看看Vue實例化經歷的過程

Vue的構造函數

咱們知道Vue的構造函數在src/core/instance/index.js中,不明白的能夠去看上一篇文章 Vue源碼學習筆記一。那咱們關注一下Vue的構造函數的內容:vue

// src/core/instance/index.js

import { initMixin } from './init'

// Vue的構造函數
function Vue (options) {
  //... 驗證環境
  this._init(options)
}

// 在Vue原型上綁定實例方法
initMixin(Vue)  // init
stateMixin(Vue)  // $set $delete $watch
eventsMixin(Vue)  // $on $once $off $emit
lifecycleMixin(Vue)  // _update $forceUpdate $destroy
renderMixin(Vue)  // $nextTick _render

添加Vue屬性和方法

這邊咱們能夠看到Vue的構造函數中執行了init方法,從下方得知init是在src\core\instance\init.js中導出的initMixin函數中定義的node

initMixin

  1. vmthis ,同時爲實例添加一個惟一的uid vm._isVue = true 監聽對象變化時用於過濾vm,由於Vue的實例是不須要監聽變化的。
// src/core/instance/init.js
Vue.prototype._init = function (options?: Object) {
 const vm: Component = this

  // 當前實例添加了一個惟一的uid
  vm._uid = uid++   

  // ...

  // 監聽對象變化時用於過濾vm
  vm._isVue = true
  
  //...
  }
  1. 參數處理,根據咱們的小栗子,咱們的options處理直接進入了else,而後對參數進行合併,這裏是對vue extend的參數須要進行合併處理,咱們這裏resolveConstructorOptions 返回的便是constructor.options自己
  2. 生命週期相關變量初始化 initLifecycle(vm)
// src\core\instance\lifecycle.js
 
 // 爲組件掛載相應屬性,並初始化
  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

4.vm 事件監聽初始化 initEvents()web

// src/core/instance/events.js
export function initEvents (vm: Component) {
  // 建立事件對象,用於存儲事件
  vm._events = Object.create(null)
  // 系統事件標識位
  vm._hasHookEvent = false
  
  // init parent attached events npm 
  // 將父組件模板中註冊的事件放到當前組件實例的listeners
  const listeners = vm.$options._parentListeners
  if (listeners) {
    updateComponentListeners(vm, listeners)
  }
}
initEvents(vm)
  initRender(vm)
  callHook(vm, 'beforeCreate')
  initInjections(vm)  

  //  vm狀態初始化,prop/data/computed/method/watch都在這裏初始化完成,vue實例create的關鍵
  initState(vm)
  initProvide(vm)  
  callHook(vm, 'created')

stateMixin

Vue實例方法--數據,該文件對應的是Vue的數據的處理,首先對$data進行掛載,而後設置數據 $set、刪除數據 $delete、觀測數據 $watch方法掛載
// stateMixin(Vue)    src/core/instance/state.js
export function stateMixin (Vue: Class<Component>) {
  // data
  const dataDef = {}
  dataDef.get = function () { return this._data }
  // prop
  const propsDef = {}
  propsDef.get = function () { return this._props }

  // ...

  // 定義$data & prop屬性
  Object.defineProperty(Vue.prototype, '$data', dataDef)
  Object.defineProperty(Vue.prototype, '$props', propsDef)

  // 原型鏈添加函數set 和 delete
  Vue.prototype.$set = set
  Vue.prototype.$delete = del

  // 原型鏈添加函數$watch
  Vue.prototype.$watch = function (){
    // ...
  }
}

eventsMixin

Vue實例方法--事件,該文件主要掛載Vue實例方法的事件,監聽事件 on once、移除事件 off、觸發事件 emit的掛載
// eventsMixin(Vue)    src/core/instance/events.js
export function eventsMixin (Vue: Class<Component>) {
  Vue.prototype.$on = function (event: string, fn: Function): Component {
    // ...
  } 
  Vue.prototype.$once = function (event: string, fn: Function): Component {
    // ...
  }
  Vue.prototype.$off = function (event?: string, fn?: Function): Component {
    // ...
  }
  Vue.prototype.$emit = function (event: string): Component {
    // ...
  }
}

lifecycleMixin

Vue實例方法--生命週期,,該文件主要掛載Vue實例方法中的生命週期方法,從新渲染 $forceUpdate()、銷燬實例 $destroy()
// lifecycleMixin(Vue)    src/core/instance/lifecycle.js
Vue.prototype._mount = function(){}
Vue.prototype._update = function (vnode: VNode, hydrating?: boolean) {}
Vue.prototype._updateFromParent = function(){}
Vue.prototype.$forceUpdate = function () {}
Vue.prototype.$destroy = function () {}

renderMixin

文件主要掛載Vue實例方法中的dom更新回調 $nextTick及一些其餘的render函數,後續咱們再的深挖一下
// renderMixin(Vue)    src/core/instance/render.js
Vue.prototype.$nextTick = function (fn: Function) {}
Vue.prototype._render = function (): VNode {}
Vue.prototype._s = _toString
Vue.prototype._v = createTextVNode
Vue.prototype._n = toNumber
Vue.prototype._e = createEmptyVNode
Vue.prototype._q = looseEqual
Vue.prototype._i = looseIndexOf
Vue.prototype._m = function(){}
Vue.prototype._o = function(){}
Vue.prototype._f = function resolveFilter (id) {}
Vue.prototype._l = function(){}
Vue.prototype._t = function(){}
Vue.prototype._b = function(){}
Vue.prototype._k = function(){}

全局API

上面部分,咱們對Vue的構造函數,在 src/core/instance/index.js文件中的做用進行了大致的瞭解,固然這並無結束,依據咱們 Vue源碼學習筆記一中提到的,咱們追溯到上一級 src/core/index.js
// src/core/index.js

import Vue from './instance/index'
import { initGlobalAPI } from './global-api/index'

import { isServerRendering } from 'core/util/env'
import { FunctionalRenderContext } from 'core/vdom/create-functional-component'

// 初始化全局變量
initGlobalAPI(Vue)

// 爲vue原型定義屬性 isServer  判斷是否爲服務端渲染
Object.defineProperty(Vue.prototype, '$isServer', {
  get: isServerRendering
})

// 爲vue原型定義屬性 ssrContext
Object.defineProperty(Vue.prototype, '$ssrContext', {
  get () {
    /* istanbul ignore next */
    return this.$vnode && this.$vnode.ssrContext
  }
})

Object.defineProperty(Vue, 'FunctionalRenderContext', {
  value: FunctionalRenderContext
})

Vue.version = '__VERSION__'

export default Vue

initGlobalAPI(Vue)

在Vue 構造函數上掛載靜態屬性和方法即 全局API
// src/core/global-api/index.js

export function initGlobalAPI(Vue: GlobalAPI) {
  const configDef = {}
  configDef.get = () => config
  // ...
  Object.defineProperty(Vue, 'config', configDef)
  
  Vue.util = { // Vue.util
    warn,
    extend,
    mergeOptions,
    defineReactive
  }

  Vue.set = set
  Vue.delete = del
  Vue.nextTick = nextTick

  Vue.options = Object.create(null)
  ASSET_TYPES.forEach(type => {
    Vue.options[type + 's'] = Object.create(null)
  })

  Vue.options._base = Vue

  extend(Vue.options.components, builtInComponents)
  
  initUse(Vue)   // Vue.use

  initMixin(Vue)  // Vue.mixin

  initExtend(Vue) // Vue.extend
  
  initAssetRegisters(Vue) // Vue.component Vue.directive Vue.filter
}

內置組件&命令

在追溯到上一級,在文件 src/platforms/web/runtime/index.js,該文件註冊了一些 Vue內置的組件:包裹動態組件 KeepAlive、元素過渡效果 Transition、多個元素過渡 TransitionGroup
// src/platforms/web/runtime/index.js 執行後

// 安裝平臺特定的utils
Vue.config.isUnknownElement = isUnknownElement
Vue.config.isReservedTag = isReservedTag
Vue.config.getTagNamespace = getTagNamespace
Vue.config.mustUseProp = mustUseProp
// 安裝平臺特定的 指令 和 組件
Vue.options = {
    components: {
        KeepAlive,
        Transition,
        TransitionGroup
    },
    directives: {
        model,
        show
    },
    filters: {},
    _base: Vue
}
Vue.prototype.__patch__
Vue.prototype.$mount

compiler編譯器添加

再上一級爲 src/platforms/web/entry-runtime-with-compiler.js,該文件對原來的 Vue.prototype.$mount進行覆蓋定義,而且在Vue上掛載了 compile。給Vue的 $mount 方法添加 compiler 編譯器,支持 template。
// src/platforms/web/entry-runtime-with-compiler.js
const mount = Vue.prototype.$mount

// ...
 
Vue.prototype.$mount = function (){

//... 覆蓋 Vue.prototype.$mount
}

// ...

//在 Vue 上掛載 compile 
//compileToFunctions 函數的做用,就是將模板 template 編譯爲render函數。
Vue.compile = compileToFunctions

總結

至此的話咱們從宏觀上過了一下從咱們一層層找到vue到一層層往外看到對Vue的添加屬性方法等,咱們有了一個總體的概念npm

  1. src/core/instance/index.js vue的構造函數,添加Vue屬性和方法
  2. src/core/index.js 全局API的掛載
  3. src/platforms/web/runtime/index.js 主要是添加web平臺特有的配置、組件和指令
  4. web/entry-runtime-with-compiler.js 給Vue的 $mount 方法添加 compiler 編譯器,支持 template
  5. scripts/config.js 編譯入口文件
相關文章
相關標籤/搜索