国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue源碼入口文件分析(推薦)

2024-05-06 15:22:16
字體:
來源:轉載
供稿:網友

開發vue項目有段時間了, 之前用angularjs 后來用 reactjs 但是那時候一直沒有時間把自己看源碼的思考記錄下來,現在我不想再浪費這 來之不易的思考, 我要堅持!!

看源碼我個人感覺非常開心,每每看上一段,自己就充實許多,不知道你是否和我一樣。

vue 源碼是眾多module(模塊)用 rollup 工具合并而成, 從package.json 中能夠看到。現在讓我們從github上下載vue項目,開始我們今天的“思考”。

我下載的源碼版本是:"version": "2.5.7",

源碼起始位置從這里可以看到

"dev": "rollup -w -c build/config.js --environment TARGET:web-full-dev"http:// 從build/config.js 中找到 TARGET: web-full-dev 這是運行和編譯(支持現在的瀏覽器,由于里面大量應用了ES6-7)后的 // Runtime+compiler development build (Browser) 'web-full-dev': {  entry: resolve('web/entry-runtime-with-compiler.js'),  dest: resolve('dist/vue.js'),  format: 'umd',  env: 'development',  alias: { he: './entity-decoder' },  banner },

找到了開始文件就是 "web/entry-runtime-with-compiler.js", 然后我們一路找 Vue 對象 終于在 “instance/index.js” 中找到了:

// 這是Vue 的開始位置function Vue (options) { // 判斷如果是不是生產環境,且不是通過new關鍵字來創建對象的話,就在控制臺打印一個warning 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)}

看似到這里都結束了,因為我們目的就是找到開始位置,但是我有個疑問,為什么Vue需要這么多層 ?

entry-runtime-with-compiler.js->runtime/index.js->core/index.js->instance/index.js

當我仔細看了源碼后恍然大悟,我們先看看他們這些文件都做了什么:

(1)instance/index.js

從Vue 模塊命名中能看出一些端倪, instance (實例) 。

這個文件是Vue 對象的開始,同時也是Vue 原型鏈(prototype) 方法的集中文件

// _initinitMixin(Vue)// $set、$delete、$watchstateMixin(Vue)// $on、$once、$off、$emiteventsMixin(Vue)// _update、$forceUpdate、$destroylifecycleMixin(Vue)// $nextTick、_render、以及多個內部調用的方法renderMixin(Vue)

這些方法只有實例化了才能調用。

(2)core/index.js

這個文件在Instance/index.js 創建和初步加工后,再次加工。 那他主要做了什么呢? 我們不考慮運行環境

initGlobalAPI(Vue)

對,就調用了這個方法,很簡單明了吧 --- "初始化全局接口",

讓我們走進initGlobalAPI 方法

export function initGlobalAPI (Vue: GlobalAPI) { // config const configDef = {} configDef.get = () => config // 在 非生產環境,如何修改了配置文件config里面的內容會提示警告 if (process.env.NODE_ENV !== 'production') {  configDef.set = () => {   warn(    'Do not replace the Vue.config object, set individual fields instead.'   )  } } // 定義config 屬性, 監聽變化 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) // 給vue 創建 ASSET_TYPES 的 空對象 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) // Vue.use initUse(Vue) // Vue.mixin initMixin(Vue) // Vue.extend initExtend(Vue) // Vue.component, Vue.directive, Vue.filter initAssetRegisters(Vue)}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 道孚县| 尉氏县| 贺兰县| 珠海市| 罗江县| 柏乡县| 蒲城县| 论坛| 龙陵县| 清徐县| 崇文区| 图木舒克市| 五莲县| 博野县| 延川县| 昌邑市| 扎鲁特旗| 新绛县| 灵台县| 松原市| 沛县| 本溪市| 民乐县| 汶川县| 依兰县| 和平县| 剑川县| 仲巴县| 米林县| 东辽县| 黄大仙区| 新和县| 昭平县| 博湖县| 民和| 永善县| 苏尼特右旗| 连云港市| 宁河县| 樟树市| 襄汾县|