詳解vue-router 初始化時(shí)做了什么
最近因?yàn)闃I(yè)務(wù)需要,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的前端 router,正好也來看一下 vue router 是怎么實(shí)現(xiàn)的。這次先來一起看看 vue-router 初始化時(shí)做了什么。
vue router 的初始化使用步驟
我們首先來看 vue-router 的使用步驟,然后再分別去看各個(gè)步驟都發(fā)生了什么。
使用 vue-router 需要經(jīng)過一下幾個(gè)步驟:
引入 vue-router:
import VueRouter from 'vue-router';
利用 vue 的插件機(jī)制,加載 vue-router:
Vue.use(VueRouter);
實(shí)例化 VueRouter:
const router = new VueRouter({
routes
})
實(shí)例化 Vue:
const app = new Vue({
router
}).$mount('#app');
Vue 的插件機(jī)制
vue 提供了一個(gè) use 方法,來加載插件:
Vue.use = function (plugin: Function | Object) {
const installedPlugins = (this._installedPlugins || (this._installedPlugins = []));
if (installedPlugins.indexOf(plugin) > -1) {
return this;
}
// additional parameters
const args = toArray(arguments, 1);
args.unshift(this);
if (typeof plugin.install === 'function') {
plugin.install.apply(plugin, args);
} else if (typeof plugin === 'function') {
plugin.apply(null, args);
}
installedPlugins.push(plugin);
return this;
}
該方法首先檢查插件是否已經(jīng)加載,如果已經(jīng)加載,直接返回 this。
如果沒有加載過,會(huì)取所有的參數(shù),并將 this 放在第一個(gè)。優(yōu)先執(zhí)行 plugin.install 方法,若不能執(zhí)行,則直接執(zhí)行 plugin 自身。
最后將 plugin push 到插件列表中。
那么我們就需要看 VueRouter 的 install 方法做了什么,VueRouter 類定義在 src/index.js 文件中。
利用 vue 的插件機(jī)制,加載 vue-router
入口文件 index.js 對(duì)外 export 了一個(gè) VueRouter 類。VueRouter 類包含了 router 的各種方法,我們直接先來看一下 install 方法。
install 方法在 index.js 中綁定在 VueRouter 類上:
import { install } from './install'
VueRouter.install = install
它的實(shí)際實(shí)現(xiàn)是在 ./install.js 中,install 方法主要做了以下幾個(gè)事情:
1、設(shè)置了兩個(gè) mixin:beforeCreate 和 destroyed。
Vue.mixin({
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)
}
})
2、在 Vue 上綁定 $route 和 $router。
Object.defineProperty(Vue.prototype, '$router', {
get () { return this._routerRoot._router }
})
Object.defineProperty(Vue.prototype, '$route', {
get () { return this._routerRoot._route }
})
3、注冊(cè)兩個(gè)組件,View 和 Link。
Vue.component('RouterView', View)
Vue.component('RouterLink', Link)
4、設(shè)置 beforeRouteEnter、beforeRouteLeave 和 beforeRouteUpdate 的 merge 策略。merge 策略的介紹可以見 這里 ,簡(jiǎn)單來說就是有重復(fù)的值時(shí)如何合并。
const strats = Vue.config.optionMergeStrategies // use the same hook merging strategy for route hooks strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created
實(shí)例化 VueRouter
我們來看一下 VueRouter 的構(gòu)造函數(shù)。首先,constructor 會(huì)初始化一些屬性:
this.app = null this.apps = [] this.options = options this.beforeHooks = [] this.resolveHooks = [] this.afterHooks = [] this.matcher = createMatcher(options.routes || [], this)
其中 matcher 比較重要,后面會(huì)詳細(xì)說。
之后會(huì)決定使用哪種模式:
let mode = options.mode || 'hash'
this.fallback = mode === 'history' && !supportsPushState && options.fallback !== false
if (this.fallback) {
mode = 'hash'
}
if (!inBrowser) {
mode = 'abstract'
}
this.mode = mode
switch (mode) {
case 'history':
this.history = new HTML5History(this, options.base)
break
case 'hash':
this.history = new HashHistory(this, options.base, this.fallback)
break
case 'abstract':
this.history = new AbstractHistory(this, options.base)
break
default:
if (process.env.NODE_ENV !== 'production') {
assert(false, `invalid mode: ${mode}`)
}
}
由于 history 模式中的pushstate方法還有一些瀏覽器沒有支持。history 模式在瀏覽器不支持時(shí)會(huì)回退到hash模式。
之后根據(jù)不同模式選擇實(shí)例化不同模式的history類,可以看到 hash 模式和 history 模式分別對(duì)應(yīng)了 HashHistory 和 HTML5History 兩個(gè)類。
此外,如果是服務(wù)器端渲染,需要進(jìn)行 router 匹配來獲取要渲染的頁面。此時(shí)服務(wù)器環(huán)境中沒有history api,因此要自行抽象實(shí)現(xiàn)一個(gè),就是 AbstractHistory。
實(shí)例化 Vue
實(shí)例化為Vue 類時(shí),會(huì)將 VueRouter 的實(shí)例傳入,這個(gè)變量放在 this.$options.router 中。由于 vue router 時(shí)以插件形式引入的,因此 這個(gè) this.$options.router 還是給 vue router 自身來用的。
vue router 初始化所做的事情就是這些,下篇博客我們來一起看一下 vue router 實(shí)際運(yùn)行時(shí)發(fā)生了什么。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用WebSocket+SpringBoot+Vue搭建簡(jiǎn)易網(wǎng)頁聊天室的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用WebSocket+SpringBoot+Vue搭建簡(jiǎn)易網(wǎng)頁聊天室的實(shí)現(xiàn),具體的步驟如下,需要的朋友可以參考下2023-03-03
vue.js?el-table虛擬滾動(dòng)完整實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于el-table虛擬滾動(dòng)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue.js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-12-12
vue組件之間數(shù)據(jù)傳遞的方法實(shí)例分析
這篇文章主要介紹了vue組件之間數(shù)據(jù)傳遞的方法,結(jié)合實(shí)例形式分析了vue.js父組件與子組件之間數(shù)據(jù)傳遞相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
vue-element-admin+flask實(shí)現(xiàn)數(shù)據(jù)查詢項(xiàng)目的實(shí)例代碼
這篇文章主要介紹了vue-element-admin+flask實(shí)現(xiàn)數(shù)據(jù)查詢項(xiàng)目,填寫數(shù)據(jù)庫連接信息和查詢語句,即可展示查詢到的數(shù)據(jù),需要的朋友可以參考下2022-11-11
vue實(shí)現(xiàn)過渡動(dòng)畫Message消息提示組件示例詳解
這篇文章主要為大家介紹了vue實(shí)現(xiàn)過渡動(dòng)畫Message消息提示組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue 實(shí)現(xiàn)網(wǎng)頁截圖功能詳解
這篇文章主要介紹了通過vue實(shí)現(xiàn)網(wǎng)頁截圖的功能,有興趣的童鞋可以了解一下2021-11-11
vue-router路由與頁面間導(dǎo)航實(shí)例解析
vue-router 是一個(gè)插件,需要在 Vue 的全局引用中通過 Vue.use()將它引用到 Vue 實(shí)例當(dāng)中。接下來通過本文給大家分享vue-router路由與頁面間導(dǎo)航,需要的朋友參考下吧2017-11-11
vue3實(shí)現(xiàn)動(dòng)態(tài)路由及菜單
這篇文章主要介紹了vue3實(shí)現(xiàn)動(dòng)態(tài)路由及菜單,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

