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

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 14:27:33
字體:
來源:轉載
供稿:網友

開發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)}

這里面基本都是 靜態方法,即:用 Vue. xxx 的形式調用。

(3)runtime/index.js

這里就加一些擴展和 在 Vue.prototype上添加了__patch__和$mount(掛載元素)。

// Vue.options.directives(model和show)和 Vue.options.components(Transition和TransitionGroup)extend(Vue.options.directives, platformDirectives)extend(Vue.options.components, platformComponents)// install platform patch functionVue.prototype.__patch__ = inBrowser ? patch : noop// public mount methodVue.prototype.$mount = function ( el?: string | Element, hydrating?: boolean): Component { el = el && inBrowser ? query(el) : undefined return mountComponent(this, el, hydrating)}

(4)entry-runtime-with-compiler.js

就干了一件事就是重寫$mount, Vue根據不同運行環境,重寫不同$mount

const mount = Vue.prototype.$mountVue.prototype.$mount = function ( el?: string | Element, hydrating?: boolean): Component { ... return mount.call(this, el, hydrating)}

總結:

到此我們找到了開始執行的文件,和每個文件有什么用,具體怎么做的,做了什么我會下次再寫。不過我們剛開始不要太在乎每個細節,不要非得弄懂每一行代碼,如果那樣,真的太累了,而且可能沒有勇氣堅持下去。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 囊谦县| 普定县| 河源市| 宝山区| 房山区| 伽师县| 乳山市| 嘉义市| 满洲里市| 余干县| 长宁区| 称多县| 张家川| 普安县| 台中县| 唐山市| 泽库县| 沁源县| 阿巴嘎旗| 平湖市| 永定县| 青神县| 桑植县| 武义县| 灵丘县| 安福县| 南川市| 甘肃省| 武川县| 彭州市| 清徐县| 梨树县| 阜新市| 永仁县| 定西市| 西充县| 西华县| 沈阳市| 大埔区| 炎陵县| 谢通门县|