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() } } })

  

posted @ 2020-09-22 15:26  小蘑菇123  阅读(3693)  评论(0编辑  收藏  举报