当前位置:网站首页>router.beforEach

router.beforEach

2022-08-11 11:51:00 -加油

未完伪代码

import Vue from 'vue'
import Router from 'vue-router'
import xxx from 'xxx.vue'
Vue.use(Router)
const routes = [
    {
    
        path:'/xxx',
        name:'xxx',
        component:xxx
    },
]
const router = new Router({
    
    mode: 'history',
    routes
  })
router.beforEach((to,from,next)=>{
    
    //获取token的方法可以进行封装
   const token = window.sessionStorage.getItem('token')
   //没有登录 且 不是访问登录页
   if(!token&&to.path!=='/login'){
    
       //游客可访问的页面
       if(hasAccessBeLogined(to.path)){
    
            next()
       }else{
    
           next('/login')
       }
   }
   //没有登录 去登录
   else if(!token&&to.path==='/login'){
    
       next()
   }
   //登陆了 去登录页
   else if(token&&to.path==='/login'){
    
       next()
   }else{
    
       //登录了 且没去登录页 需要进行权限鉴定
       //还没实现
   }
})

export default router
原网站

版权声明
本文为[-加油]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_51040174/article/details/125011144