亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue?入口與?initGlobalAPI實(shí)例剖析

 更新時(shí)間:2022年08月30日 16:41:10   作者:RiemannHypothesis  
這篇文章主要為大家介紹了Vue?入口與?initGlobalAPI實(shí)例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Vue 的入口

在上面的scripts/alias文件中可以分析出入口是

src/platforms/web/entry-runtime-with-compiler.js

import Vue from './runtime/index'

在這個(gè)入口 JS 的上方我們可以找到 Vue 的來(lái)源:import Vue from './runtime/index',我們先來(lái)看一下這塊兒的實(shí)現(xiàn),它定義在 src/platforms/web/runtime/index.js 中:

import Vue from 'core/index'

這里關(guān)鍵的代碼是 import Vue from 'core/index',之后的邏輯都是對(duì) Vue 這個(gè)對(duì)象做一些擴(kuò)展,可以先不用看,我們來(lái)看一下真正初始化 Vue 的地方,在 src/core/index.js 中:

import Vue from './instance/index'

這里有 2 處關(guān)鍵的代碼,import Vue from './instance/index' 和 initGlobalAPI(Vue),初始化全局 Vue API(我們稍后介紹),我們先來(lái)看第一部分,在 src/core/instance/index.js 中:

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 的廬山真面目,它實(shí)際上就是一個(gè)用 Function 實(shí)現(xiàn)的類,我們只能通過(guò) new Vue 去實(shí)例化它。

我們往后看這里有很多 xxxMixin 的函數(shù)調(diào)用,并把 Vue 當(dāng)參數(shù)傳入,它們的功能都是給 Vue 的 prototype 上擴(kuò)展一些方法,Vue 按功能把這些擴(kuò)展分散到多個(gè)模塊中去實(shí)現(xiàn),而不是在一個(gè)模塊里實(shí)現(xiàn)所有

initGlobalAPI

Vue.js 在整個(gè)初始化過(guò)程中,除了給它的原型 prototype 上擴(kuò)展方法,還會(huì)給 Vue 這個(gè)對(duì)象本身擴(kuò)展全局的靜態(tài)方法,它的定義在 src/core/global-api/index.js 中:

/* @flow */
import config from '../config'
import { initUse } from './use'
import { initMixin } from './mixin'
import { initExtend } from './extend'
import { initAssetRegisters } from './assets'
import { set, del } from '../observer/index'
import { ASSET_TYPES } from 'shared/constants'
import builtInComponents from '../components/index'
import { observe } from 'core/observer/index'
import {
  warn,
  extend,
  nextTick,
  mergeOptions,
  defineReactive
} from '../util/index'
export function initGlobalAPI (Vue: GlobalAPI) {
  // config
  const configDef = {}
  configDef.get = () => config
  if (process.env.NODE_ENV !== 'production') {
    configDef.set = () => {
      warn(
        'Do not replace the Vue.config object, set individual fields instead.'
      )
    }
  }
  Object.defineProperty(Vue, 'config', configDef)
  // exposed util methods.
  // NOTE: these are not considered part of the public API - avoid relying on
  // them unless you are aware of the risk.
  Vue.util = {
    warn,
    extend,
    mergeOptions,
    defineReactive
  }
  Vue.set = set
  Vue.delete = del
  Vue.nextTick = nextTick
  // 2.6 explicit observable API
  Vue.observable = <T>(obj: T): T => {
    observe(obj)
    return obj
  }
  Vue.options = Object.create(null)
  ASSET_TYPES.forEach(type => {
    Vue.options[type + 's'] = Object.create(null)
  })
  // this is used to identify the "base" constructor to extend all plain-object
  // components with in Weex's multi-instance scenarios.
  Vue.options._base = Vue
  extend(Vue.options.components, builtInComponents)
  initUse(Vue)
  initMixin(Vue)
  initExtend(Vue)
  initAssetRegisters(Vue)
}

這里就是在 Vue 上擴(kuò)展的一些全局方法的定義,Vue 官網(wǎng)中關(guān)于全局 API 都可以在這里找到

以上就是Vue 入口與 initGlobalAPI實(shí)例剖析的詳細(xì)內(nèi)容,更多關(guān)于Vue 入口 initGlobalAPI的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論