Vue?入口與?initGlobalAPI實例剖析
Vue 的入口
在上面的scripts/alias文件中可以分析出入口是
src/platforms/web/entry-runtime-with-compiler.js
import Vue from './runtime/index'
在這個入口 JS 的上方我們可以找到 Vue 的來源:import Vue from './runtime/index',我們先來看一下這塊兒的實現(xiàn),它定義在 src/platforms/web/runtime/index.js 中:
import Vue from 'core/index'
這里關鍵的代碼是 import Vue from 'core/index',之后的邏輯都是對 Vue 這個對象做一些擴展,可以先不用看,我們來看一下真正初始化 Vue 的地方,在 src/core/index.js 中:
import Vue from './instance/index'
這里有 2 處關鍵的代碼,import Vue from './instance/index' 和 initGlobalAPI(Vue),初始化全局 Vue API(我們稍后介紹),我們先來看第一部分,在 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 的廬山真面目,它實際上就是一個用 Function 實現(xiàn)的類,我們只能通過 new Vue 去實例化它。
我們往后看這里有很多 xxxMixin 的函數(shù)調(diào)用,并把 Vue 當參數(shù)傳入,它們的功能都是給 Vue 的 prototype 上擴展一些方法,Vue 按功能把這些擴展分散到多個模塊中去實現(xiàn),而不是在一個模塊里實現(xiàn)所有
initGlobalAPI
Vue.js 在整個初始化過程中,除了給它的原型 prototype 上擴展方法,還會給 Vue 這個對象本身擴展全局的靜態(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 上擴展的一些全局方法的定義,Vue 官網(wǎng)中關于全局 API 都可以在這里找到
以上就是Vue 入口與 initGlobalAPI實例剖析的詳細內(nèi)容,更多關于Vue 入口 initGlobalAPI的資料請關注腳本之家其它相關文章!
相關文章
前端elementUI?select選擇器實現(xiàn)遠程搜索
這篇文章主要為大家介紹了前端使用elementUI?select選擇器實現(xiàn)遠程搜索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
three.js實現(xiàn)vr全景圖功能實例(vue)
去年全景圖在微博上很是火爆了一陣,正好我也做過一點全景相關的項目,下面這篇文章主要給大家介紹了關于three.js實現(xiàn)vr全景圖功能的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-05-05
Vue 實現(xiàn)高級穿梭框 Transfer 封裝過程
本文介紹了基于Vue2和Element-UI實現(xiàn)的高級穿梭框組件Transfer的設計與技術(shù)方案,組件支持多項選擇,并能實時同步已選擇項,包含豎版和橫版設計稿,并提供了組件的使用方法和源碼,此組件具備本地分頁和搜索功能,適用于需要在兩個列表間進行數(shù)據(jù)選擇和同步的場景2024-09-09
vue3如何將html元素變成canvas(海報生成),進行圖片保存/截圖
這篇文章主要介紹了vue3實現(xiàn)將html元素變成canvas(海報生成),進行圖片保存/截圖,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
一文詳解Vue3的watch是如何實現(xiàn)監(jiān)聽的
watch這個API大家都很熟悉,今天這篇文章小編來帶你搞清楚Vue3的watch是如何實現(xiàn)對響應式數(shù)據(jù)進行監(jiān)聽的,希望對大家有一定的幫助2024-11-11

