国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue-router鉤子執(zhí)行順序

這篇具有很好參考價(jià)值的文章主要介紹了vue-router鉤子執(zhí)行順序。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Vue的路由在執(zhí)行跳轉(zhuǎn)時(shí),根據(jù)源碼可知,調(diào)用了router中定義的navigate函數(shù)

function push(to: RouteLocationRaw) {
   return pushWithRedirect(to)
}

function replace(to: RouteLocationRaw) {
   return push(assign(locationAsObject(to), { replace: true }))
}

function pushWithRedirect(
    to: RouteLocationRaw | RouteLocation,
    redirectedFrom?: RouteLocation
  ): Promise<NavigationFailure | void | undefined> {
    // ...

    return (failure ? Promise.resolve(failure) : navigate(toLocation, from))/*調(diào)用navigate*/
      .catch((error: NavigationFailure | NavigationRedirectError) =>
        isNavigationFailure(error)
          ? // navigation redirects still mark the router as ready
            isNavigationFailure(error, ErrorTypes.NAVIGATION_GUARD_REDIRECT)
            ? error
            : markAsReady(error) // also returns the error
          : // reject any unknown error
            triggerError(error, toLocation, from)
      )
      .then((failure: NavigationFailure | NavigationRedirectError | void) => {
        if (failure) {
          // ...
        } else {
          // 執(zhí)行finalizeNavigation完成導(dǎo)航
          // if we fail we don't finalize the navigation
          failure = finalizeNavigation(
            toLocation as RouteLocationNormalizedLoaded,
            from,
            true,
            replace,
            data
          )
        }
        // 觸發(fā)`afterEach`
        triggerAfterEach(
          toLocation as RouteLocationNormalizedLoaded,
          from,
          failure
        )
        return failure
      })
}

function navigate(
    to: RouteLocationNormalized,
    from: RouteLocationNormalizedLoaded
  ): Promise<any> {
    let guards: Lazy<any>[]

    // ...

    // run the queue of per route beforeRouteLeave guards
    return (
      // 1.調(diào)用離開組件的`beforeRouteLeave`鉤子
      runGuardQueue(guards)
        .then(() => {
          // 獲取全局的的`beforeEach`鉤子
          // check global guards beforeEach
          guards = []
          for (const guard of beforeGuards.list()) {
            guards.push(guardToPromiseFn(guard, to, from))
          }
          guards.push(canceledNavigationCheck)

          // 2.調(diào)用全局的`beforeEach`鉤子
          return runGuardQueue(guards)
        })
        .then(() => {
          // 獲取更新的路由其對應(yīng)組件的`beforeRouteUpdate`鉤子
          // check in components beforeRouteUpdate
          guards = extractComponentsGuards(
            updatingRecords,
            'beforeRouteUpdate',
            to,
            from
          )

          for (const record of updatingRecords) {
            record.updateGuards.forEach(guard => {
              guards.push(guardToPromiseFn(guard, to, from))
            })
          }
          guards.push(canceledNavigationCheck)

          // 3.調(diào)用復(fù)用組件的`beforeRouteUpdate`鉤子
          // run the queue of per route beforeEnter guards
          return runGuardQueue(guards)
        })
        .then(() => {
          // 獲取進(jìn)入的路由自身的`beforeEnter`鉤子
          // check the route beforeEnter
          guards = []
          for (const record of enteringRecords) {
            // do not trigger beforeEnter on reused views
            if (record.beforeEnter) {
              if (isArray(record.beforeEnter)) {
                for (const beforeEnter of record.beforeEnter)
                  guards.push(guardToPromiseFn(beforeEnter, to, from))
              } else {
                guards.push(guardToPromiseFn(record.beforeEnter, to, from))
              }
            }
          }
          guards.push(canceledNavigationCheck)

          // 4.調(diào)用新匹配路由的`beforeEnter`鉤子
          // run the queue of per route beforeEnter guards
          return runGuardQueue(guards)
        })
        .then(() => {
          // NOTE: at this point to.matched is normalized and does not contain any () => Promise<Component>

          // clear existing enterCallbacks, these are added by extractComponentsGuards
          to.matched.forEach(record => (record.enterCallbacks = {}))

          // 獲取進(jìn)入的路由其對應(yīng)組件的`beforeRouteEnter`鉤子
          // check in-component beforeRouteEnter
          guards = extractComponentsGuards(
            enteringRecords,
            'beforeRouteEnter',
            to,
            from
          )
          guards.push(canceledNavigationCheck)

          // 5.調(diào)用新組件的`beforeRouteEnter`鉤子
          // run the queue of per route beforeEnter guards
          return runGuardQueue(guards)
        })
        .then(() => {
          // 獲取全局的的`beforeResolve`鉤子
          // check global guards beforeResolve
          guards = []
          for (const guard of beforeResolveGuards.list()) {
            guards.push(guardToPromiseFn(guard, to, from))
          }
          guards.push(canceledNavigationCheck)

          // 6.調(diào)用全局的`beforeResolve`守衛(wèi)
          return runGuardQueue(guards)
        })
        // catch any navigation canceled
        .catch(err =>
          isNavigationFailure(err, ErrorTypes.NAVIGATION_CANCELLED)
            ? err
            : Promise.reject(err)
        )
    )
  }
  
  // 觸發(fā)`afterEach`
  function triggerAfterEach(
    to: RouteLocationNormalizedLoaded,
    from: RouteLocationNormalizedLoaded,
    failure?: NavigationFailure | void
  ): void {
    // navigation is confirmed, call afterGuards
    // TODO: wrap with error handlers
    afterGuards
      .list()
      .forEach(guard => runWithContext(() => guard(to, from, failure)))
  }

由上述源碼中可以看出,由Promise then的鏈?zhǔn)秸{(diào)用保證了路由守衛(wèi)按照以下順序執(zhí)行:

  1. 舊的路由組件beforeRouteLeave
  2. 全局配置的beforeEach
  3. 復(fù)用的路由組件beforeRouteUpdate
  4. 新路由的beforeEnter
  5. 新路由組件的beforeRouteEnter
  6. 全局配置的beforeResolve
  7. navigate執(zhí)行完畢后,會(huì)調(diào)用triggerAfterEach函數(shù),觸發(fā)afterEach

和官網(wǎng)文檔上所寫的是一樣的。The Full Navigation Resolution Flow文章來源地址http://www.zghlxwxcb.cn/news/detail-607452.html

到了這里,關(guān)于vue-router鉤子執(zhí)行順序的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 利用vue-router跳轉(zhuǎn)的幾種方式

    ?1 router-link 2 this.$router.push ? ? 跳轉(zhuǎn)到指定路徑,并將跳轉(zhuǎn)頁面壓入history棧中,也就是添加了一個(gè)頁面記錄。 3 this.$router.replace ? ? 跳轉(zhuǎn)到指定路徑,將history棧中的當(dāng)前頁面替換為跳轉(zhuǎn)到的頁面。 4 this.$router.go(n) ? ? 在histroy棧中向前或者向后跳轉(zhuǎn)n個(gè)頁面,n可為正整數(shù)或負(fù)

    2024年02月12日
    瀏覽(26)
  • 登錄頁面的實(shí)現(xiàn)及跳轉(zhuǎn)(vue-router)

    登錄頁面的實(shí)現(xiàn)及跳轉(zhuǎn)(vue-router)

    路由的核心:改變URL,頁面不會(huì)整體刷新 一、創(chuàng)建項(xiàng)目 1、使用vite創(chuàng)建項(xiàng)目 注意:根據(jù)需求,選擇‘可選功能’完成安裝(具體安裝步驟vue.md) 2、項(xiàng)目結(jié)構(gòu) 3、創(chuàng)建登錄項(xiàng)目 1創(chuàng)建一個(gè)組件(登錄頁面),我們把這個(gè)組件稱為單文件組件 位置:(規(guī)范情況下,將組件寫到

    2023年04月22日
    瀏覽(23)
  • 路由vue-router

    路由vue-router

    路由(英文:router)就是 對應(yīng)關(guān)系 SPA 指的是一個(gè) web 網(wǎng)站只有 唯一的一個(gè) HTML 頁面 ,所有組件的展示與切換都在這唯一的一個(gè)頁面內(nèi)完成。 此時(shí),不同組件之間的切換需要通過前端路由來實(shí)現(xiàn)。 結(jié)論: 在 SPA 項(xiàng)目中,不同功能之間的切換 ,要依賴于 前端路由 來完成!

    2024年02月07日
    瀏覽(25)
  • vue-router路由守衛(wèi)

    在我們使用vue-router的時(shí)候,路由守衛(wèi)就像監(jiān)聽器、攔截器一樣,幫我們做一些鑒權(quán)等操作,vue中的路由守衛(wèi)分為全局路由守衛(wèi)、獨(dú)享路由守衛(wèi)、組件內(nèi)的路由守衛(wèi) 全局路由守衛(wèi) :?beforeEach、 afterEach 組件獨(dú)享路由守衛(wèi) :beforeEnter、 beforeLeave 組件內(nèi)路由守衛(wèi) :beforeRouteEnter、

    2024年02月11日
    瀏覽(17)
  • 【Vue-Router】路由入門

    【Vue-Router】路由入門

    路由(Routing)是指確定網(wǎng)站或應(yīng)用程序中特定頁面的方式。在Web開發(fā)中,路由用于根據(jù)URL的不同部分來確定應(yīng)用程序中應(yīng)該顯示哪個(gè)內(nèi)容。 構(gòu)建前端項(xiàng)目 安裝依賴和路由 3. router 使用 login.vue reg.vue index.ts App.vue main.ts router-view 補(bǔ)充: router-view 是Vue Router提供的一個(gè)組件,用于

    2024年02月13日
    瀏覽(28)
  • Vue 解決route-link路由跳轉(zhuǎn)時(shí)頁面不刷新問題

    1.分析 在使用route-link跳轉(zhuǎn)路由時(shí),每次跳轉(zhuǎn),頁面都不會(huì)刷新。是因?yàn)槁酚商D(zhuǎn)并非頁面刷新,而我們獲取數(shù)據(jù)的方法都是寫在created鉤子函數(shù)中,所以,created并不會(huì)每次都執(zhí)行,從而導(dǎo)致頁面數(shù)據(jù)沒有刷新。 2.解決方法 在router-view標(biāo)簽中加入:key=\\\"$route.fullPath\\\"即可

    2024年02月11日
    瀏覽(24)
  • vue-router路由懶加載

    vue-router路由懶加載

    路由懶加載指的是打包部署時(shí)將資源按照對應(yīng)的頁面進(jìn)行劃分,需要的時(shí)候加載對應(yīng)的頁面資源,而不是把所有的頁面資源打包部署到一塊。避免不必要資源加載。(參考vue項(xiàng)目實(shí)現(xiàn)路由按需加載(路由懶加載)的3種方式_小胖梅的博客-CSDN博客_vue懶加載?) 這里有三種方式可以

    2023年04月08日
    瀏覽(21)
  • vue-router路由模式詳解

    vue-router路由模式詳解

    目錄 一. vue-router(前端路由)有兩種模式,hash模式和history模式 二、路由模式解析 三、兩種模式的區(qū)別 1、hash模式 ?2、history路由 (3)popstate實(shí)現(xiàn)history路由攔截,監(jiān)聽頁面返回事件 一. vue-router(前端路由)有兩種模式,hash模式和history模式 1.hash 就是指 url 后面的 # 號(hào)以及后

    2024年02月03日
    瀏覽(34)
  • 【Vue-Router】路由傳參

    【Vue-Router】路由傳參

    list.json login.vue reg.vue App.vue index.ts reg.vue item?.name ,item?.price ,item?.id ,他們?nèi)绻皇褂每蛇x鏈操作符會(huì)出現(xiàn)報(bào)錯(cuò): \\\'__VLS_ctx.item\\\' is possibly \\\'undefined\\\'. login.vue 注意: 傳遞 params 參數(shù)時(shí),若使用 to 的對象寫法,必須使用 name 配置項(xiàng),不能用 path 。 傳遞 params 參數(shù)時(shí),需要提前在規(guī)

    2024年02月13日
    瀏覽(20)
  • vue-router(路由)詳細(xì)教程

    路由是一個(gè)比較廣義和抽象的概念,路由的本質(zhì)就是 對應(yīng)關(guān)系 。 在開發(fā)中,路由分為: ? 后端路由 ? 前端路由 后端路由 概念:根據(jù)不同的用戶 URL 請求,返回不同的內(nèi)容 本質(zhì):URL 請求地址與服務(wù)器資源之間的對應(yīng)關(guān)系 [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將

    2024年02月04日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包