const routes = [
{
// 访问根路径的时候跳转到login
path: '/',
redirect: '/login'
},
{
path: '/login',
name:'login',
component: LoginView
},
{
path:'/register',
component:Register
},
{
path:'/password',
component:UpdatePassword
},
{
path:'/home',
component:()=>
import('../components/Home.vue'),
redirect:'/Dupchecking',
children:[{
path:'/Dupchecking',
component:()=>
import('../components/Dupchecking.vue')
},
{
path:'/Usermanager',
component:()=>
import('../components/Usermanager.vue')
},
{
path:'/Statistics',
component:()=>
import('../components/Statistics.vue')
},
]
},
{
path: "/:pathMatch(.*)", // 页面不存在的情况下会跳到404页面
name: 'notFound',
component:()=>
import('../components/Page404.vue')
}
]
router.beforeEach((to,from,next)=>{
const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
if(!userInfo && to.name !== 'login'){
next({name:'login'})
}else{
next()
}
})
设置了路由守卫之后,注册和修改密码(箭头指的位置)点击没有反应,跳转不了页面,这个路由守卫如何修改?
全局守卫写错了
应该定义不需要被登录拦截的路由数组,
比如
const notFilterComponents = ['login', 'register', 'findPassword', '404']
router.beforeEach((to, from, next) => {
const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
if (!userInfo && notFilterComponents.indexOf(to.name || '') < 0) {
next({ name: 'login' })
} else {
next()
})
您的采纳就是我最大的动力,谢谢!!!
router.beforeEach((to,from,next)=>{
const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
if(!userInfo && to.name !== 'login'&&to.path!=='register'&&to.path!=='password'){ //这里需要添加上过滤掉注册和修改密码,遇到这俩个页面直接走next
next({name:'login'})
}else{
next()
}
})
在判断的时候加上过滤掉注册和修改密码,也可以给每个路由项定义meta去判断
router.beforeEach((to,from,next)=>{
const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
if(to.path==='register' || to.path==='password'){
next()
return;
}
if(!userInfo && to.name !== 'login'){
next({name:'login'})
}else{
next()
}
})