詳解Vue-Router源碼分析路由實現(xiàn)原理
深入Vue-Router源碼分析路由實現(xiàn)原理
使用Vue開發(fā)SPA應(yīng)用,離不開vue-router,那么vue和vue-router是如何協(xié)作運行的呢,下面從使用的角度,大白話幫大家一步步梳理下vue-router的整個實現(xiàn)流程。
到發(fā)文時使用的版本是:
- vue (v2.5.0)
- vue-router (v3.0.1)
一、vue-router 源碼結(jié)構(gòu)
github 地址:https://github.com/vuejs/vue-router

components下是兩個組件<router-view> 和 <router-link>
history是路由方式的封裝,提供三種方式
util下主要是各種功能類和功能函數(shù)
create-matcher和create-router-map是生成匹配表
index是VueRouter類,也整個插件的入口
Install 提供安裝的方法
先整體展示下vue-router使用方式,請牢記一下幾步哦。
import Vue from 'vue'
import VueRouter from 'vue-router'
//注冊插件 如果是在瀏覽器環(huán)境運行的,可以不寫該方法
Vue.use(VueRouter)
// 1. 定義(路由)組件。
// 可以從其他文件 import 進來
const User = { template: '<div>用戶</div>' }
const Role = { template: '<div>角色</div>' }
// 2. 定義路由
// Array,每個路由應(yīng)該映射一個組件。
const routes = [
{ path: '/user', component: User },
{ path: '/home', component: Home }
]
// 3. 創(chuàng)建 router 實例,并傳 `routes` 配置
const router = new VueRouter({
routes
})
// 4. 創(chuàng)建和掛載根實例。
// 記得要通過 router 對象以參數(shù)注入Vue,
// 從而讓整個應(yīng)用都有路由功能
// 使用 router-link 組件來導(dǎo)航.
// 路由出口
// 路由匹配到的組件將渲染在這里
const app = new Vue({
router,
template: `
<div id="app">
<h1>Basic</h1>
<ul>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/user">用戶</router-link></li>
<li><router-link to="/role">角色</router-link></li>
<router-link tag="li" to="/user">/用戶</router-link>
</ul>
<router-view class="view"></router-view>
</div>
`
}).$mount('#app')
分析開始
第一步
Vue是使用.use( plugins )方法將插件注入到Vue中。
use方法會檢測注入插件VueRouter內(nèi)的install方法,如果有,則執(zhí)行install方法。
注意:如果是在瀏覽器環(huán)境,在index.js內(nèi)會自動調(diào)用.use方法。如果是基于node環(huán)境,需要手動調(diào)用。
if (inBrowser && window.Vue) {
window.Vue.use(VueRouter)
}
Install解析 (對應(yīng)目錄結(jié)構(gòu)的install.js)
該方法內(nèi)主要做了以下三件事:
- 1、對Vue實例混入beforeCreate鉤子操作(在Vue的生命周期階段會被調(diào)用)
- 2、通過Vue.prototype定義router、router、route 屬性(方便所有組件可以獲取這兩個屬性)
- 3、Vue上注冊router-link和router-view兩個組件
export function install (Vue) {
if (install.installed && _Vue === Vue) return
install.installed = true
_Vue = Vue
const isDef = v => v !== undefined
const registerInstance = (vm, callVal) => {
let i = vm.$options._parentVnode
if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
i(vm, callVal)
}
}
Vue.mixin({
//對Vue實例混入beforeCreate鉤子操作
beforeCreate () {
if (isDef(this.$options.router)) {
this._routerRoot = this
this._router = this.$options.router
this._router.init(this)
Vue.util.defineReactive(this, '_route', this._router.history.current)
} else {
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
}
registerInstance(this, this)
},
destroyed () {
registerInstance(this)
}
})
//通過Vue.prototype定義$router、$route 屬性(方便所有組件可以獲取這兩個屬性)
Object.defineProperty(Vue.prototype, '$router', {
get () { return this._routerRoot._router }
})
Object.defineProperty(Vue.prototype, '$route', {
get () { return this._routerRoot._route }
})
//Vue上注冊router-link和router-view兩個組件
Vue.component('RouterView', View)
Vue.component('RouterLink', Link)
const strats = Vue.config.optionMergeStrategies
// use the same hook merging strategy for route hooks
strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created
}
第二步 生成router實例
const router = new VueRouter({
routes
})
生成實例過程中,主要做了以下兩件事
- 1、根據(jù)配置數(shù)組(傳入的routes)生成路由配置記錄表。
- 2、根據(jù)不同模式生成監(jiān)控路由變化的History對象
注:History類由HTML5History、HashHistory、AbstractHistory三類繼承
history/base.js實現(xiàn)了基本history的操作
history/hash.js,history/html5.js和history/abstract.js繼承了base,只是根據(jù)不同的模式封裝了一些基本操作
第三步 生成vue實例
const app = new Vue({
router,
template: `
<div id="app">
<h1>Basic</h1>
<ul>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/user">用戶</router-link></li>
<li><router-link to="/role">角色</router-link></li>
<router-link tag="li" to="/user">/用戶</router-link>
</ul>
<router-view class="view"></router-view>
</div>
`
}).$mount('#app')
代碼執(zhí)行到這,會進入Vue的生命周期,還記得第一步Vue-Router對Vue混入了beforeCreate鉤子嗎,在此會執(zhí)行哦
Vue.mixin({
beforeCreate () {
//驗證vue是否有router對象了,如果有,就不再初始化了
if (isDef(this.$options.router)) { //沒有router對象
//將_routerRoot指向根組件
this._routerRoot = this
//將router對象掛載到根組件元素_router上
this._router = this.$options.router
//初始化,建立路由監(jiān)控
this._router.init(this)
//劫持數(shù)據(jù)_route,一旦_route數(shù)據(jù)發(fā)生變化后,通知router-view執(zhí)行render方法
Vue.util.defineReactive(this, '_route', this._router.history.current)
} else {
//如果有router對象,去尋找根組件,將_routerRoot執(zhí)行根組件(解決嵌套關(guān)系時候_routerRoot指向不一致問題)
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
}
registerInstance(this, this)
},
destroyed () {
registerInstance(this)
}
})
代碼執(zhí)行到這,初始化結(jié)束,界面將顯示默認首頁
路由更新方式:
一、主動觸發(fā)
router-link綁定了click方法,觸發(fā)history.push或者history.replace,從而觸發(fā)history.transitionTo。
transitionTo用于處理路由轉(zhuǎn)換,其中包含了updateRoute用于更新_route。
在beforeCreate中有劫持_route的方法,當_route變化后,觸發(fā)router-view的變化。
二、地址變化(如:在瀏覽器地址欄直接輸入地址)
HashHistory和HTML5History會分別監(jiān)控hashchange和popstate來對路由變化作對用的處理 。
HashHistory和HTML5History捕獲到變化后會對應(yīng)執(zhí)行push或replace方法,從而調(diào)用transitionTo
,剩下的就和上面主動觸發(fā)一樣啦。
總結(jié)
1、安裝插件
混入beforeCreate生命周期處理,初始化_routerRoot,_router,_route等數(shù)據(jù)
全局設(shè)置vue靜態(tài)訪問router和router和route,方便后期訪問
完成了router-link和 router-view 兩個組件的注冊,router-link用于觸發(fā)路由的變化,router-view作 為功能組件,用于觸發(fā)對應(yīng)路由視圖的變化
2、根據(jù)路由配置生成router實例
根據(jù)配置數(shù)組生成路由配置記錄表
生成監(jiān)控路由變化的hsitory對象
3、將router實例傳入根vue實例
根據(jù)beforeCreate混入,為根vue對象設(shè)置了劫持字段_route,用戶觸發(fā)router-view的變化
調(diào)用init()函數(shù),完成首次路由的渲染,首次渲染的調(diào)用路徑是 調(diào)用history.transitionTo方法,根據(jù)router的match函數(shù),生成一個新的route對象
接著通過confirmTransition對比一下新生成的route和當前的route對象是否改變,改變的話觸發(fā)updateRoute,更新hsitory.current屬性,觸發(fā)根組件的_route的變化,從而導(dǎo)致組件的調(diào)用render函數(shù),更新router-view
另外一種更新路由的方式是主動觸發(fā)
router-link綁定了click方法,觸發(fā)history.push或者history.replace,從而觸發(fā)history.transitionTo
同時會監(jiān)控hashchange和popstate來對路由變化作對用的處理
以上所述是小編給大家介紹的Vue-Router源碼分析路由實現(xiàn)原理詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解使用mpvue開發(fā)github小程序總結(jié)
這篇文章主要介紹了詳解使用mpvue開發(fā)github小程序總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Vue3發(fā)送post請求出現(xiàn)400?Bad?Request報錯的解決辦法
這篇文章主要給大家介紹了關(guān)于Vue3發(fā)送post請求出現(xiàn)400?Bad?Request報錯的解決辦法,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-02-02
vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式
這篇文章主要介紹了vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

