Vue?入口與?initGlobalAPI實(shí)例剖析
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)的類,我們只能通過 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è)初始化過程中,除了給它的原型 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)文章
前端elementUI?select選擇器實(shí)現(xiàn)遠(yuǎn)程搜索
這篇文章主要為大家介紹了前端使用elementUI?select選擇器實(shí)現(xiàn)遠(yuǎn)程搜索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
three.js實(shí)現(xiàn)vr全景圖功能實(shí)例(vue)
去年全景圖在微博上很是火爆了一陣,正好我也做過一點(diǎn)全景相關(guān)的項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于three.js實(shí)現(xiàn)vr全景圖功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
Vue 實(shí)現(xiàn)高級(jí)穿梭框 Transfer 封裝過程
本文介紹了基于Vue2和Element-UI實(shí)現(xiàn)的高級(jí)穿梭框組件Transfer的設(shè)計(jì)與技術(shù)方案,組件支持多項(xiàng)選擇,并能實(shí)時(shí)同步已選擇項(xiàng),包含豎版和橫版設(shè)計(jì)稿,并提供了組件的使用方法和源碼,此組件具備本地分頁(yè)和搜索功能,適用于需要在兩個(gè)列表間進(jìn)行數(shù)據(jù)選擇和同步的場(chǎng)景2024-09-09
Vue實(shí)現(xiàn)簡(jiǎn)單可擴(kuò)展甘特圖的方法詳解
Ganttastic是一個(gè)小型的Vue.js組件,用于在Web應(yīng)用程序上呈現(xiàn)一個(gè)可配置的、可拖動(dòng)的甘特圖。本文就將用它來(lái)實(shí)現(xiàn)簡(jiǎn)單可擴(kuò)展的甘特圖,感興趣的可以嘗試一下2022-11-11
vue3如何將html元素變成canvas(海報(bào)生成),進(jìn)行圖片保存/截圖
這篇文章主要介紹了vue3實(shí)現(xiàn)將html元素變成canvas(海報(bào)生成),進(jìn)行圖片保存/截圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Vue 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鼠標(biāo)拖拽滾動(dòng)效果插件
這篇文章主要介紹了Vue 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鼠標(biāo)拖拽滾動(dòng)效果插件,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
一文詳解Vue3的watch是如何實(shí)現(xiàn)監(jiān)聽的
watch這個(gè)API大家都很熟悉,今天這篇文章小編來(lái)帶你搞清楚Vue3的watch是如何實(shí)現(xiàn)對(duì)響應(yīng)式數(shù)據(jù)進(jìn)行監(jiān)聽的,希望對(duì)大家有一定的幫助2024-11-11

