摘要:有一點(diǎn)要注意的是,暴露的方法最好不要依賴,因?yàn)樗赡芙?jīng)常會(huì)發(fā)生變化,是不穩(wěn)定的。
從入口開(kāi)始
我們之前提到過(guò) Vue.js 構(gòu)建過(guò)程,在 web 應(yīng)用下,我們來(lái)分析 Runtime + Compiler 構(gòu)建出來(lái)的 Vue.js,它的入口是 src/platforms/web/entry-runtime-with-compiler.js:
摘選entry-runtime-with-compiler.js
import config from "core/config" import { warn, cached } from "core/util/index" import { mark, measure } from "core/util/perf" //主角在這里 import Vue from "./runtime/index" import { query } from "./util/index" import { compileToFunctions } from "./compiler/index" import { shouldDecodeNewlines, shouldDecodeNewlinesForHref } from "./util/compat"Vue 的入口
在這個(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" import config from "core/config" import { extend, noop } from "shared/util"
這里關(guān)鍵的代碼是 import Vue from "core/index"是真正初始化 Vue 的地方[/src/core/index.js]
import Vue from "./instance/index" import { initGlobalAPI } from "./global-api/index"
這里有 2 處關(guān)鍵的代碼,import Vue from "./instance/index"(./指的是當(dāng)前目錄) 和 initGlobalAPI(Vue),初始化全局 Vue API,我們先來(lái)看第一部分,在 src/core/instance/index.js 中:
Vue 的定義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
1
在這里,我們終于看到了 Vue 的廬山真面目,它實(shí)際上就是一個(gè)用 Function 實(shí)現(xiàn)的類,我們只能通過(guò) new Vue 去實(shí)例化它
為何 Vue 不用 ES6 的 Class 去實(shí)現(xiàn)呢?我們往后看這里有很多 xxxMixin 的函數(shù)調(diào)用,并把 Vue 當(dāng)參數(shù)傳入,它們的功能都是給 Vue 的 prototype 上擴(kuò)展一些方法(這里具體的細(xì)節(jié)會(huì)在之后的文章介紹,這里不展開(kāi)),Vue 按功能把這些擴(kuò)展分散到多個(gè)模塊中去實(shí)現(xiàn),而不是在一個(gè)模塊里實(shí)現(xiàn)所有,這種方式是用 Class 難以實(shí)現(xiàn)的。這么做的好處是非常方便代碼的維護(hù)和管理,這種編程技巧也非常值得我們?nèi)W(xué)習(xí)。
initGlobalAPIVue.js 在整個(gè)初始化過(guò)程中,除了給它的原型 prototype 上擴(kuò)展方法,還會(huì)給 Vue 這個(gè)對(duì)象本身擴(kuò)展全局的靜態(tài)方法,它的定義在 src/core/global-api/index.js 中:
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 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 都可以在這里找到,這里不會(huì)介紹細(xì)節(jié),會(huì)在之后的章節(jié)我們具體介紹到某個(gè) API 的時(shí)候會(huì)詳細(xì)介紹。有一點(diǎn)要注意的是,Vue.util 暴露的方法最好不要依賴,因?yàn)樗赡芙?jīng)常會(huì)發(fā)生變化,是不穩(wěn)定的。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/99355.html
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥(niǎo)雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門(mén),久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥(niǎo)雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門(mén),久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:特意對(duì)前端學(xué)習(xí)資源做一個(gè)匯總,方便自己學(xué)習(xí)查閱參考,和好友們共同進(jìn)步。 特意對(duì)前端學(xué)習(xí)資源做一個(gè)匯總,方便自己學(xué)習(xí)查閱參考,和好友們共同進(jìn)步。 本以為自己收藏的站點(diǎn)多,可以很快搞定,沒(méi)想到一入?yún)R總深似海。還有很多不足&遺漏的地方,歡迎補(bǔ)充。有錯(cuò)誤的地方,還請(qǐng)斧正... 托管: welcome to git,歡迎交流,感謝star 有好友反應(yīng)和斧正,會(huì)及時(shí)更新,平時(shí)業(yè)務(wù)工作時(shí)也會(huì)不定期更...
閱讀 2945·2021-11-04 16:06
閱讀 777·2021-09-30 09:56
閱讀 1843·2021-09-22 10:02
閱讀 2623·2019-08-29 13:43
閱讀 2220·2019-08-29 13:42
閱讀 2302·2019-08-29 12:21
閱讀 1059·2019-08-29 11:29
閱讀 1389·2019-08-26 13:51