vue router 的初始化使用步驟前端
咱們首先來看 vue-router 的使用步驟,而後再分別去看各個步驟都發生了什麼。vue
使用 vue-router 須要通過一下幾個步驟:vue-router
引入 vue-router:api
import VueRouter from 'vue-router';
利用 vue 的插件機制,加載 vue-router:瀏覽器
Vue.use(VueRouter);
實例化 VueRouter:服務器
const router = new VueRouter({ routes })
實例化 Vue:app
const app = new Vue({ router }).$mount('#app');
Vue 的插件機制函數
vue 提供了一個 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; } //前端全棧學習交流圈:866109386 //面向1-3經驗前端開發人員 //幫助突破技術瓶頸,提高思惟能力。
該方法首先檢查插件是否已經加載,若是已經加載,直接返回 this。this
若是沒有加載過,會取全部的參數,並將 this 放在第一個。優先執行 plugin.install 方法,若不能執行,則直接執行 plugin 自身。
最後將 plugin push 到插件列表中。
那麼咱們就須要看 VueRouter 的 install 方法作了什麼,VueRouter 類定義在 src/index.js 文件中。
利用 vue 的插件機制,加載 vue-router
入口文件 index.js 對外 export 了一個 VueRouter 類。VueRouter 類包含了 router 的各類方法,咱們直接先來看一下 install 方法。
install 方法在 index.js 中綁定在 VueRouter 類上:
import { install } from './install' VueRouter.install = install
它的實際實現是在 ./install.js 中,install 方法主要作了如下幾個事情:
一、設置了兩個 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) } })
二、在 Vue 上綁定 $route 和 $router。
Object.defineProperty(Vue.prototype, '$router', { get () { return this._routerRoot._router } }) Object.defineProperty(Vue.prototype, '$route', { get () { return this._routerRoot._route } })
三、註冊兩個組件,View 和 Link。
Vue.component('RouterView', View) Vue.component('RouterLink', Link)
四、設置 beforeRouteEnter、beforeRouteLeave 和 beforeRouteUpdate 的 merge 策略。merge 策略的介紹能夠見 這裏 ,簡單來講就是有重複的值時如何合併。
const strats = Vue.config.optionMergeStrategies // use the same hook merging strategy for route hooks strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created
實例化 VueRouter
咱們來看一下 VueRouter 的構造函數。首先,constructor 會初始化一些屬性:
this.app = null this.apps = [] this.options = options this.beforeHooks = [] this.resolveHooks = [] this.afterHooks = [] this.matcher = createMatcher(options.routes || [], this)
其中 matcher 比較重要,後面會詳細說。
以後會決定使用哪一種模式:
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 模式在瀏覽器不支持時會回退到hash模式。
以後根據不一樣模式選擇實例化不一樣模式的history類,能夠看到 hash 模式和 history 模式分別對應了 HashHistory 和 HTML5History 兩個類。
此外,若是是服務器端渲染,須要進行 router 匹配來獲取要渲染的頁面。此時服務器環境中沒有history api,所以要自行抽象實現一個,就是 AbstractHistory。
實例化 Vue
實例化爲Vue 類時,會將 VueRouter 的實例傳入,這個變量放在this.$options.router
中。因爲 vue router 時以插件形式引入的,所以 這個 this.$options.router 仍是給 vue router 自身來用的。