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

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

 // 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類來說,主要是下下面兩個函數(shù)的邏輯

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 => { // 異常處理函數(shù)
      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()
    }
    <!-- 根據(jù)當前路由對象和匹配的路由:返回更新的路由、激活的路由、停用的路由 -->
    const {
      updated,
      deactivated,
      activated
    } = resolveQueue(this.current.matched, route.matched)
    <!-- 需要執(zhí)行的任務隊列 -->
    const queue: Array<?NavigationGuard> = [].concat(
      // beforeRouteLeave 鉤子函數(shù)
      extractLeaveGuards(deactivated),
      // 全局的beforeHooks勾子
      this.router.beforeHooks,
      // beforeRouteUpdate 鉤子函數(shù)調用
      extractUpdateHooks(updated),
      // config里的勾子
      activated.map(m => m.beforeEnter),
      // async components
      resolveAsyncComponents(activated)
    )
    
    this.pending = route
    <!-- 對于queue數(shù)組所執(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 鉤子函數(shù)調用 -->
      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() })
          })
        }
      })
    })
  }

結語:

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

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

相關文章

  • Xx-vue自定義指令實現(xiàn)點擊水波紋漣漪效果

    Xx-vue自定義指令實現(xiàn)點擊水波紋漣漪效果

    這篇文章主要為大家介紹了Xx-vue自定義指令實現(xiàn)點擊水波紋漣漪效果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • IntelliJ IDEA編輯器配置vue高亮顯示

    IntelliJ IDEA編輯器配置vue高亮顯示

    這篇文章主要為大家詳細介紹了IntelliJ IDEA編輯器配置vue高亮顯示,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Angular和Vue雙向數(shù)據(jù)綁定的實現(xiàn)原理(重點是vue的雙向綁定)

    Angular和Vue雙向數(shù)據(jù)綁定的實現(xiàn)原理(重點是vue的雙向綁定)

    這篇文章主要介紹了Angular和Vue雙向數(shù)據(jù)綁定的實現(xiàn)原理(重點是vue的雙向綁定),非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-11-11
  • Vue3新特性Suspense和Teleport應用場景分析

    Vue3新特性Suspense和Teleport應用場景分析

    本文介紹了Vue2和Vue3中的Suspense用于處理異步請求的加載提示,以及如何在組件間實現(xiàn)動態(tài)加載,同時,Teleport技術展示了如何在DOM中靈活地控制組件的渲染位置,解決布局問題,感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • vue使用i18n實現(xiàn)國際化的方法詳解

    vue使用i18n實現(xiàn)國際化的方法詳解

    這篇文章主要給大家介紹了關于vue使用i18n如何實現(xiàn)國際化的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • Vue.js + Nuxt.js 項目中使用 Vee-validate 表單校驗

    Vue.js + Nuxt.js 項目中使用 Vee-validate 表單校驗

    vee-validate 是為 Vue.js 量身打造的表單校驗框架,允許您校驗輸入的內容并顯示對應的錯誤提示信息。這篇文章給大家?guī)砹薞ue.js 使用 Vee-validate 實現(xiàn)表單校驗的相關知識,感興趣的朋友一起看看吧
    2019-04-04
  • Element Badge標記的使用方法

    Element Badge標記的使用方法

    這篇文章主要介紹了Element Badge標記的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Vue中如何實現(xiàn)輪播圖的示例代碼

    Vue中如何實現(xiàn)輪播圖的示例代碼

    本篇文章主要介紹了Vue中如何實現(xiàn)輪播圖的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Vue實現(xiàn)父子組件傳值其實不難

    Vue實現(xiàn)父子組件傳值其實不難

    這篇文章主要介紹了Vue實現(xiàn)父子組件傳值其實不難問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Vue3中關于ref和reactive的區(qū)別分析

    Vue3中關于ref和reactive的區(qū)別分析

    這篇文章主要介紹了vue3關于ref和reactive的區(qū)別分析,文中通過示例代碼介紹的非常詳細,具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-06-06

最新評論

旌德县| 大足县| 故城县| 美姑县| 浦县| 赫章县| 晋中市| 扎兰屯市| 大关县| 平山县| 定边县| 农安县| 尤溪县| 甘孜| 九龙城区| 镇原县| 吉隆县| 青州市| 晋宁县| 剑阁县| 聊城市| 游戏| 新邵县| 中牟县| 乐亭县| 巴南区| 营山县| 正镶白旗| 九江县| 云梦县| 台湾省| 九龙县| 腾冲县| 菏泽市| 桃源县| 屏东市| 五大连池市| 虹口区| 杭锦后旗| 浦江县| 卫辉市|