最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解vue-router 初始化時(shí)做了什么

 更新時(shí)間:2018年06月11日 08:33:40   作者:梅旭光的個(gè)人博客  
這篇文章主要介紹了詳解vue-router 初始化時(shí)做了什么,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

最近因?yàn)闃I(yè)務(wù)需要,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的前端 router,正好也來(lái)看一下 vue router 是怎么實(shí)現(xiàn)的。這次先來(lái)一起看看 vue-router 初始化時(shí)做了什么。

vue router 的初始化使用步驟

我們首先來(lái)看 vue-router 的使用步驟,然后再分別去看各個(gè)步驟都發(fā)生了什么。

使用 vue-router 需要經(jīng)過(guò)一下幾個(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 方法,來(lái)加載插件:

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。

如果沒(méi)有加載過(guò),會(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 的各種方法,我們直接先來(lái)看一下 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) 這里 ,簡(jiǎn)單來(lái)說(shuō)就是有重復(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

我們來(lái)看一下 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ì)說(shuō)。

之后會(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方法還有一些瀏覽器沒(méi)有支持。history 模式在瀏覽器不支持時(shí)會(huì)回退到hash模式。

之后根據(jù)不同模式選擇實(shí)例化不同模式的history類,可以看到 hash 模式和 history 模式分別對(duì)應(yīng)了 HashHistory 和 HTML5History 兩個(gè)類。

此外,如果是服務(wù)器端渲染,需要進(jìn)行 router 匹配來(lái)獲取要渲染的頁(yè)面。此時(shí)服務(wù)器環(huán)境中沒(méi)有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 自身來(lái)用的。

vue router 初始化所做的事情就是這些,下篇博客我們來(lái)一起看一下 vue router 實(shí)際運(yùn)行時(shí)發(fā)生了什么。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

武冈市| 兴国县| 方正县| 积石山| 和政县| 上犹县| 承德市| 佛山市| 呼和浩特市| 丰城市| 莎车县| 齐河县| 岑巩县| 高碑店市| 定西市| 运城市| 资溪县| 台前县| 类乌齐县| 旌德县| 周宁县| 玉山县| 团风县| 商水县| 松原市| 乌审旗| 西乌珠穆沁旗| 镇原县| 甘德县| 乐业县| 安丘市| 林口县| 宁蒗| 错那县| 航空| 芦山县| 平和县| 平阴县| 玉龙| 兴文县| 浑源县|