欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue-router源碼之history類的淺析

 更新時間:2019年05月21日 09:09:33   作者:fuqihan  
這篇文章主要介紹了vue-router源碼之history類的淺析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

當前版本: 3.0.3

類目錄: src/history/base.js

前言:

對于vue-router來說,有三種路由模式history,hash,abstract, abstract是運行在沒有window的環(huán)境下的,這三種模式都是繼承于history類,history實現(xiàn)了一些共用的方法,對于一開始看vue-router源碼來說,可以從這里開始看起。

初始屬性

router: Router; 表示VueRouter實例。實例化History類時的第一個參數
 base: string;  表示基路徑。會用normalizeBase進行規(guī)范化。實例化History類時的第二個參數。
 current: Route; 表示當前路由(route)。
 pending: ?Route; 描述阻塞狀態(tài)。
 cb: (r: Route) => void; 監(jiān)聽時的回調函數。
 ready: boolean; 描述就緒狀態(tài)。
 readyCbs: Array<Function>; 就緒狀態(tài)的回調數組。
 readyErrorCbs: Array<Function>; 就緒時產生錯誤的回調數組。
 errorCbs: Array<Function>; 錯誤的回調數組

 // implemented by sub-classes
 <!-- 下面幾個是需要子類實現(xiàn)的方法,這里就先不說了,之后寫其他類實現(xiàn)的時候分析 -->
 +go: (n: number) => void;
 +push: (loc: RawLocation) => void;
 +replace: (loc: RawLocation) => void;
 +ensureURL: (push?: boolean) => void;
 +getCurrentLocation: () => string;

對于history類來說,主要是下下面兩個函數的邏輯

transitionTo

這個方法主要是對路由跳轉的封裝, location接收的是HTML5History,HashHistory,AbstractHistory, onComplete是成功的回調,onAbort是失敗的回調

transitionTo (location: RawLocation, onComplete?: Function, onAbort?: Function) {
  const route = this.router.match(location, this.current) // 解析成每一個location需要的route
  this.confirmTransition(route, () => {
   this.updateRoute(route)
   onComplete && onComplete(route)
   this.ensureURL()

   // fire ready cbs once
   if (!this.ready) {
    this.ready = true
    this.readyCbs.forEach(cb => { cb(route) })
   }
  }, err => {
   if (onAbort) {
    onAbort(err)
   }
   if (err && !this.ready) {
    this.ready = true
    this.readyErrorCbs.forEach(cb => { cb(err) })
   }
  })
 }

confirmTransition

這是方法是確認跳轉,route是匹配的路由對象, onComplete是匹配成功的回調, 是匹配失敗的回調

confirmTransition(route: Route, onComplete: Function, onAbort?: Function) {
    const current = this.current
    const abort = err => { // 異常處理函數
      if (isError(err)) {
        if (this.errorCbs.length) {
          this.errorCbs.forEach(cb => { cb(err) })
        } else {
          warn(false, 'uncaught error during route navigation:')
          console.error(err)
        }
      }
      onAbort && onAbort(err)
    }
    if (
      isSameRoute(route, current) &&
      // in the case the route map has been dynamically appended to
      route.matched.length === current.matched.length
    ) {
      this.ensureURL()
      return abort()
    }
    <!-- 根據當前路由對象和匹配的路由:返回更新的路由、激活的路由、停用的路由 -->
    const {
      updated,
      deactivated,
      activated
    } = resolveQueue(this.current.matched, route.matched)
    <!-- 需要執(zhí)行的任務隊列 -->
    const queue: Array<?NavigationGuard> = [].concat(
      // beforeRouteLeave 鉤子函數
      extractLeaveGuards(deactivated),
      // 全局的beforeHooks勾子
      this.router.beforeHooks,
      // beforeRouteUpdate 鉤子函數調用
      extractUpdateHooks(updated),
      // config里的勾子
      activated.map(m => m.beforeEnter),
      // async components
      resolveAsyncComponents(activated)
    )
    
    this.pending = route
    <!-- 對于queue數組所執(zhí)行的迭代器方法 -->
    const iterator = (hook: NavigationGuard, next) => {
      if (this.pending !== route) {
        return abort()
      }
      try {
        hook(route, current, (to: any) => {
          if (to === false || isError(to)) {
            // next(false) -> abort navigation, ensure current URL
            this.ensureURL(true)
            abort(to)
          } else if (
            typeof to === 'string' ||
            (typeof to === 'object' && (
              typeof to.path === 'string' ||
              typeof to.name === 'string'
            ))
          ) {
            // next('/') or next({ path: '/' }) -> redirect
            abort()
            if (typeof to === 'object' && to.replace) {
              this.replace(to)
            } else {
              this.push(to)
            }
          } else {
            // confirm transition and pass on the value
            next(to)
          }
        })
      } catch (e) {
        abort(e)
      }
    }
    
    runQueue(queue, iterator, () => {
      const postEnterCbs = []
      const isValid = () => this.current === route
      <!-- beforeRouteEnter 鉤子函數調用 -->
      const enterGuards = extractEnterGuards(activated, postEnterCbs, isValid)
      const queue = enterGuards.concat(this.router.resolveHooks)
      <!-- 迭代運行queue -->
      runQueue(queue, iterator, () => {
        if (this.pending !== route) {
          return abort()
        }
        this.pending = null
        onComplete(route)
        if (this.router.app) {
          this.router.app.$nextTick(() => {
            postEnterCbs.forEach(cb => { cb() })
          })
        }
      })
    })
  }

結語:

每一次總結,都是對之前讀源碼的再一次深入的了解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • nginx部署多個vue項目的方法示例

    nginx部署多個vue項目的方法示例

    這篇文章主要介紹了nginx部署多個vue項目的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 詳解vue 數據傳遞的方法

    詳解vue 數據傳遞的方法

    這篇文章給大家介紹了vue數據傳遞的方法,包括,父組件向子組件傳值 ,子組件向父組件傳值 ,路由傳值等多種方法,需要的朋友參考下
    2018-04-04
  • vue從零實現(xiàn)一個消息通知組件的方法詳解

    vue從零實現(xiàn)一個消息通知組件的方法詳解

    這篇文章主要介紹了vue從零實現(xiàn)一個消息通知組件的方法,結合實例形式分析了vue實現(xiàn)消息通知組件的具體原理、實現(xiàn)步驟、與相關操作技巧,需要的朋友可以參考下
    2020-03-03
  • 使用vue項目配置多個代理的注意點

    使用vue項目配置多個代理的注意點

    這篇文章主要介紹了使用vue項目配置多個代理的注意點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Vue路由切換頁面不更新問題解決方案

    Vue路由切換頁面不更新問題解決方案

    這篇文章主要介紹了Vue路由切換頁面不更新問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • vue自定義組件@click點擊失效問題及解決

    vue自定義組件@click點擊失效問題及解決

    這篇文章主要介紹了vue自定義組件@click點擊失效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue跳轉外部鏈接始終有l(wèi)ocalhost的問題

    vue跳轉外部鏈接始終有l(wèi)ocalhost的問題

    這篇文章主要介紹了vue跳轉外部鏈接始終有l(wèi)ocalhost的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Vue.js中的computed工作原理

    Vue.js中的computed工作原理

    這篇文章,我們通過實現(xiàn)一個簡單版的和Vue中computed具有相同功能的函數來了解computed是如何工作的。對Vue.js中的computed工作原理感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-03-03
  • Vue中Axios的封裝與接口管理詳解

    Vue中Axios的封裝與接口管理詳解

    在vue項目中和后臺交互獲取數據這塊,我們通常使用的是axios庫,它是基于promise的http庫,可運行在瀏覽器端和node.js中,下面這篇文章主要給大家介紹了關于Vue中Axios的封裝與接口管理的相關資料,需要的朋友可以參考下
    2022-03-03
  • vue實現(xiàn)移動端輕量日期組件不依賴第三方庫的方法

    vue實現(xiàn)移動端輕量日期組件不依賴第三方庫的方法

    這篇文章主要介紹了vue 移動端輕量日期組件不依賴第三方庫,需要的朋友可以參考下
    2019-04-04

最新評論