导航守卫权限管理
路由配置
router = new Router({
routes:[
{
path:"/",
name:"index",
component:Index
},
{
path:"/login",
name:"login",
component:Login
},
{
path:"/pro",
name:"pro",
component:Pro,
meta:{ requiresAuth: true }//随意写的
}
]
})
1.登陆权限
router.beforeEach(function(to,from,next){
console.log(to)
if(true){//表示登陆状态
console.log(from,to)
if(to.path =='/login'){
next("/")
}else{
next()
}
}else{
if(to.path!='/login'&& to.meta.requiresAuth){
next({path:'/login'})
}else{
next()//放行了,不能少,否则不会执行跳转
}
}
})
2.登陆权限,更完整
router.beforeEach(function(to, from, next){
if(false){ //false表示没登陆,true表示登陆
console.log(from,to)
console.log(to.path == "/login")
console.log(to.meta.requiresAuth)
if(to.path =="/"){
next()
}else {
if(to.meta.r){//表示是否需要权限验证
next()
}else{
if(to.path =="/login"){
next()
}else{
alert("没有权限,请用管理员登陆")
next({path:'/login'})}
}
}
}else{
to.path!="/login"? next({path:'/login'}):next()
}
})