使用我们的includes优化我们的导航守卫

zhuanbike 2022-2-27 661

把indexOf方法替换掉

router.beforeEach((to, from, next) => {
  const pathArr=['/standard','/user'] //设置需要权限的路径,
  if (pathArr.includes(to.Path)||to.fullPath.includes(pathArr[0]||to.fullPath.includes(pathArr[1]){ //如果to.path在上面路径内,to.path是绝对路径,to.fullPath是带参数的 
    const token = sessionStorage.getItem('user')//获取session
          if(token){//如果有session 
            next()//放行
          }else{
            next('/login')//如果没有session跳转登录页面
          }
    }else{//如果to.path没有在在上面路径内,放行
      next()
    }
  }


最新回复 (0)
发新帖