vue 路由守卫,未登录强制跳转到登录页


main.js 中,

//  挂载路由导航守卫
router.beforeEach((to, from, next) => {
   //获取token const hasToken = getToken(); // to 将要访问的路径 // from 代表从哪个路径跳转而来 // next 是一个函数,表示放行 // next() 放行 next('/login') 强制跳转 // 如果用户访问的登录页,直接放行 if (hasToken) { if (to.path === '/login') { next({ path: '/' }) NProgress.done() } next() } else { // 没有token,强制跳转到登录页 if (whiteList.indexOf(to.path) !== -1) { next() } else { next(`/login`) // next(`/login?redirect=${to.path}`) NProgress.done() } } })

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM