Vue route navigation guard

If the user is not logged in, but direct access to specific pages by URL, need to navigate to the login page, then you need to guard the route navigation, make a jump.

Configuration:

// hanging in the navigation routing guard 
// If the user is not logged in, but with a direct URL to access a particular page, you need to navigate back to the login page 
router.beforeEach ((to, from, the Next) => {
   // path to be accessed 
  / / from the path from which to jump from 
  // the Next is a function that represents the release 
  // if the landing page is accessed directly released 
  IF (to.path === '/ the login') return the Next ()
   // get token 
  const token = window.sessionStorage.token
   // If there is no token jump to the landing page 
  IF (! token) return the Next ( '/ the login' )
   // otherwise the direct release 
  the Next () 
})

 

Guess you like

Origin www.cnblogs.com/liea/p/11794468.html