vue路由守卫配合权限,白名单
router.beforeEach(async(to, from, next) => {
// 进度条开始
NProgress.start()
// 确认用户是否已登录(获取它的token值,这里的getToken()是封装好的一个方法)
const hasToken = getToken()
if (hasToken) {//如果有token,说明是登录状态
if (to.path === '/login') {//路由是/login页,那么直接跳转到首页
next({ path: '/' })
NProgress.done() //进度条结束
} else {//如果不是登录页
// 确定用户是否已通过getInfo获得其权限角色
const hasRoles = store.getters.roles && store.getters.roles.length > 0
if (hasRoles) {//如果通过角色权限,继续访问
next()
} else {//如果没有通过角色权限
// alert('没有role')
console.log('获取角色')
try {
// get user info
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
const { roles } = await store.dispatch('user/getInfo')
// generate accessible routes map based on roles
const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
// dynamically add accessible routes
router.addRoutes(accessRoutes)
// hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
next({ ...to, replace: true })
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {//如果没有token
/* has no token*/
// alert('没有token to.path=' + to.path)
if (whiteList.indexOf(to.path) !== -1) {//白名单中有的路由,可以继续访问
// in the free login whitelist, go directly
next()
} else {//否则,白名单中没有的路由,跳回首页
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
vue路由守卫配合权限,白名单的更多相关文章
- vue路由守卫用于登录验证权限拦截
vue路由守卫用于登录验证权限拦截 vue路由守卫 - 全局(router.beforeEach((to, from, next) =>来判断登录和路由跳转状态) 主要方法: to:进入到哪个路 ...
- react router @4 和 vue路由 详解(八)vue路由守卫
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 13.vue路由守卫 a.beforeEach 全局守卫 (每个路由调用前都会触发,根据 ...
- Vue | 路由守卫面试常考
前言 最近在整理基础,欢迎掘友们一起交流学习 结尾有彩蛋哦! Vue Router 路由守卫 导图目录 路由守卫分类 全局路由守卫 单个路由守卫 组件路由守卫 路由守卫执行的完整过程 路由守卫分类 全 ...
- vue路由守卫应用,监听是否登录
路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫 ...
- Vue路由守卫(跳转页面置顶的处理方)
在用Vue 框架开发时,在电脑调试没有任何问题,但是用手机调试时会发现页面跳转的不对.就是跳转时页面展示的滑动位置不对,会保留上次跳转页面时的跳转位置.因此需要对页面的路由跳转进行优化,需要用到Vue ...
- vue 路由守卫
router.beforeEach((to, from, next) => { const nextRoute = [ 'login']; var token = window.localSto ...
- vue路由守卫
路由守卫 //路由进来之时 beforeRouteEnter(to, from, next) { console.log(this, 'beforeRouteEnter'); // undefined ...
- vue路由守卫触发顺序
不同组件之间的路由跳转流程图 导航被触发(A–>B) 调用A组件内路由守卫beforeRouteLeave(to,from,next) 调用全局路由前置守卫router.beforeEach(t ...
- vue路由守卫+cookie实现页面跳转时验证用户是否登录----(二)设置路由守卫
上一篇我们已经封装好了cookie方法,登录成功之后也可以吧用户信息存到cookie中,接下来需要在router/index.js中引入一下cookie.js文件 然后继续添加以下代码 /* * be ...
随机推荐
- transform-origin属性
一.transform-origin的作用 transform-origin改变元素基点 (transform-origin是变形原点,原点就是元素绕着旋转或变形的点) 注意:该属性只有在设置了tra ...
- PHP导出excel文件名中文IE乱码解决
<?php $ua = strtolower($_SERVER['HTTP_USER_AGENT']); if(preg_match('/msie/', $ua) || preg_match(' ...
- Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)
题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...
- 爬虫 ---- BeautifulSoup的基础使用
#BeautifulSoup的基础使用from bs4 import BeautifulSoup #导入bs4库 html = "<p class='stylecss'>< ...
- DAY 5 下午
每个点一定属于一个重链 重链条数和轻边边数是logn级别 证明和启发式合并差不多 因为轻子树的大小至少是重子树大小-1 树链剖分:两遍dfs 第一次:统计子树大小,确定重儿子 第二次:把重链剖出来 每 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_5_BufferedWriter_字符缓冲输出流
使用newLine来换行 同样的效果 println的源码里面其实就用的就是newLine()
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_09 Debug调试_1_Debug追踪
行号的右边.点击就加上红色的原点 执行后程序停在断点的这一行代码 F8 Shift+F8从方法中出来. F9跳到下一个断点. 终止程序 Ctrl+F2: 断点停留在第一行之后,F8进入下一行代码. 再 ...
- c# Thread5——线程同步之基本原子操作。Mutex互斥量的使用
之前的博文也说到了如果多线程对于访问的公共资源操作都是原子操作,那么可以避免竞争条件.关于多线程的竞争可以百度. 1.执行最基本的原子操作 c#提供了一系列供我们使用的原子操作的方法和类型,比如我们的 ...
- 一些输出、处理细节&注意点
https://blog.csdn.net/qq_41071646/article/details/79953476 输出百分比的时候,结果需要加上一个EPS(1e-6)四舍五入保证精度. 卡精度—— ...
- Flask使用原生sql语句
安装pip install flask-sqlalchemy 创建数据库对象 # 设置数据库连接地址app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://r ...