SHARE
TWEET

Untitled

a guest May 22nd, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. router.beforeResolve(async (routeTo, routeFrom, next) => {
  2.   // Create a beforeResolve hook, which fires whenever
  3.   // beforeRouteEnter and beforeRouteUpdate would. This
  4.   // allows us to ensure data is fetched even when params change,
  5.   // but the resolved route does not. We put it in meta to
  6.   // indicate that it's a hook we created, rather than part of
  7.   // Vue Router (yet?).
  8.   try {
  9.     // For each matched route...
  10.     for (const route of routeTo.matched) {
  11.       await new Promise((resolve, reject) => {
  12.         // If a beforeResolve hook is defined, call it with
  13.         // the same arguments as the beforeEnter hook.
  14.         if (route.meta && route.meta.beforeResolve) {
  15.           route.meta.beforeResolve(routeTo, routeFrom, (...args) => {
  16.             // If the user chose to redirect...
  17.             if (args.length) {
  18.               // If redirecting to the same route we're coming from...
  19.               if (routeFrom.name === args[0].name) {
  20.                 // Complete the animation of the route progress bar.
  21.                 NProgress.done()
  22.               }
  23.               // Complete the redirect.
  24.               next(...args)
  25.               reject(new Error('Redirected'))
  26.             } else {
  27.               resolve()
  28.             }
  29.           })
  30.         } else {
  31.           // Otherwise, continue resolving the route.
  32.           resolve()
  33.         }
  34.       })
  35.     }
  36.     // If a beforeResolve hook chose to redirect, just return.
  37.   } catch (error) {
  38.     return
  39.   }
  40.  
  41.   // If we reach this point, continue resolving the route.
  42.   next()
  43. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top