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

vue-router源碼之history類的淺析

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

當(dāng)前版本: 3.0.3

類目錄: src/history/base.js

前言:

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

初始屬性

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

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

對(duì)于history類來說,主要是下下面兩個(gè)函數(shù)的邏輯

transitionTo

這個(gè)方法主要是對(duì)路由跳轉(zhuǎn)的封裝, location接收的是HTML5History,HashHistory,AbstractHistory, onComplete是成功的回調(diào),onAbort是失敗的回調(diào)

transitionTo (location: RawLocation, onComplete?: Function, onAbort?: Function) {
  const route = this.router.match(location, this.current) // 解析成每一個(gè)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

這是方法是確認(rèn)跳轉(zhuǎn),route是匹配的路由對(duì)象, onComplete是匹配成功的回調(diào), 是匹配失敗的回調(diào)

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ù)當(dāng)前路由對(duì)象和匹配的路由:返回更新的路由、激活的路由、停用的路由 -->
    const {
      updated,
      deactivated,
      activated
    } = resolveQueue(this.current.matched, route.matched)
    <!-- 需要執(zhí)行的任務(wù)隊(duì)列 -->
    const queue: Array<?NavigationGuard> = [].concat(
      // beforeRouteLeave 鉤子函數(shù)
      extractLeaveGuards(deactivated),
      // 全局的beforeHooks勾子
      this.router.beforeHooks,
      // beforeRouteUpdate 鉤子函數(shù)調(diào)用
      extractUpdateHooks(updated),
      // config里的勾子
      activated.map(m => m.beforeEnter),
      // async components
      resolveAsyncComponents(activated)
    )
    
    this.pending = route
    <!-- 對(duì)于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ù)調(diào)用 -->
      const enterGuards = extractEnterGuards(activated, postEnterCbs, isValid)
      const queue = enterGuards.concat(this.router.resolveHooks)
      <!-- 迭代運(yùn)行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() })
          })
        }
      })
    })
  }

結(jié)語:

每一次總結(jié),都是對(duì)之前讀源碼的再一次深入的了解

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

相關(guān)文章

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

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

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

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

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

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

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

    Vue3新特性Suspense和Teleport應(yīng)用場(chǎng)景分析

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

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

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

    Vue.js + Nuxt.js 項(xiàng)目中使用 Vee-validate 表單校驗(yàn)

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

    Element Badge標(biāo)記的使用方法

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

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

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

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

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

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

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

最新評(píng)論

淮北市| 托克逊县| 尼勒克县| 达州市| 荔浦县| 罗定市| 乡城县| 宁陕县| 民权县| 金华市| 威远县| 广丰县| 类乌齐县| 乐清市| 桐柏县| 晋江市| 桑植县| 彭山县| 徐州市| 根河市| 海门市| 阿拉善右旗| 中山市| 灯塔市| 武邑县| 河北区| 新竹市| 钟山县| 阿克陶县| 随州市| 德格县| 屯留县| 大渡口区| 天门市| 静安区| 西藏| 阿拉尔市| 连平县| 嘉鱼县| 镇安县| 专栏|