在vue中詳細介紹原始碼入口檔案(詳細教學)
這篇文章主要介紹了vue源碼入口文件分析(推薦),現在分享給大家,也給大家做個參考。
開發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" // 从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) 方法的集中檔案
// _init initMixin(Vue) // $set、$delete、$watch stateMixin(Vue) // $on、$once、$off、$emit eventsMixin(Vue) // _update、$forceUpdate、$destroy lifecycleMixin(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 inpidual 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 function Vue.prototype.__patch__ = inBrowser ? patch : noop // public mount method Vue.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.$mount Vue.prototype.$mount = function ( el?: string | Element, hydrating?: boolean ): Component { ... return mount.call(this, el, hydrating) }
上面是我整理給大家的,希望今後會對大家有幫助。
相關文章:
#以上是在vue中詳細介紹原始碼入口檔案(詳細教學)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

可以通過以下步驟為 Vue 按鈕添加函數:將 HTML 模板中的按鈕綁定到一個方法。在 Vue 實例中定義該方法並編寫函數邏輯。

Vue.js 中的 watch 選項允許開發者監聽特定數據的變化。當數據發生變化時,watch 會觸發一個回調函數,用於執行更新視圖或其他任務。其配置選項包括 immediate,用於指定是否立即執行回調,以及 deep,用於指定是否遞歸監聽對像或數組的更改。

Vue 多頁面開發是一種使用 Vue.js 框架構建應用程序的方法,其中應用程序被劃分為獨立的頁面:代碼維護性:將應用程序拆分為多個頁面可以使代碼更易於管理和維護。模塊化:每個頁面都可以作為獨立的模塊,便於重用和替換。路由簡單:頁面之間的導航可以通過簡單的路由配置來管理。 SEO 優化:每個頁面都有自己的 URL,這有助於搜索引擎優化。

Vue.js 遍歷數組和對像有三種常見方法:v-for 指令用於遍歷每個元素並渲染模板;v-bind 指令可與 v-for 一起使用,為每個元素動態設置屬性值;.map 方法可將數組元素轉換為新數組。

Vue 中 div 元素跳轉的方法有兩種:使用 Vue Router,添加 router-link 組件。添加 @click 事件監聽器,調用 this.$router.push() 方法跳轉。

實現 Vue 中 a 標籤跳轉的方法包括:HTML 模板中使用 a 標籤指定 href 屬性。使用 Vue 路由的 router-link 組件。使用 JavaScript 的 this.$router.push() 方法。可通過 query 參數傳遞參數,並在 router 選項中配置路由以進行動態跳轉。
