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 ...
随机推荐
- JavaScript变量和字面量
一.什么是变量? 首先了解一下什么是内存:内存就是保存程序在运行过程中,所需要用到的数据8bit(比特是表示信息的最小单位). 8bit=1byte 1024byte=1MB 1024MB=1GB 1 ...
- SQL server 表copy 到别一张表
SQL server 表copy 到别一张表 ------------------ INSERT INTO 表名 (表字段) SELECT 表1字段 FROM 表名2: ---------- ...
- Ubuntu 16.04下使用docker部署MySQL主从复制
(以下docker相关的命令,需要在root用户环境下或通过sudo提升权限来进行操作.) 首先更新 软件源 https://mirrors.tuna.tsinghua.edu.cn/help/ubu ...
- SetwindowText 之线程阻塞
示意代码: CriticalSection g_Section; CDialog g_Dlg; // 工作线程函数UINT TreadFunc_A(PVOID para){ Sleep(10); g_ ...
- APIO2019解题报告
「APIO 2019」奇怪装置 题目描述 有无限个二元组,每个二元组为\(((t+\left\lfloor\frac{t}{B} \right\rfloor)\%A,t \% B)\),给出一些区间, ...
- 纯css实现Material Design中的水滴动画按钮
前言 大家平时应该经常见到这种特效,很炫酷不是吗 这是谷歌Material Design中最常见的特效了,市面上也有很多现成的js库,用来模拟这一特效.但是往往要引入一大堆js和css,其实在已有的项 ...
- MySQL闪回工具之myflash 和 binlog2sql
MySQL闪回工具之:binlog2sql https://github.com/danfengcao/binlog2sql MYSQL Binglog分析利器:binlog2sql使用详解 :h ...
- Spring命名空间引入方法
spring 整合了各种工具,并且spring提供了对各种工具的xml scheme 的配置方式,简化了开发. 但是对于各种工具的xml命名空间的引入,我一直很郁闷,不知道应该怎样引入,今天经过摸索发 ...
- Delphi XE2 之 FireMonkey 入门(24) - 数据绑定: TBindingsList: TBindExpression.Direction
在学习 BindingSource 属性时, 可以让两个控件互为绑定源; TBindExpression 对应的功能是 Direction 属性. 先在窗体上添加 Edit1.Edit2.Bindin ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_02 泛型_3_定义和使用含有泛型的类
创建一个类,添加一个name的属性,然后生成get和set 使用上面创建的类 使用泛型 所以我们取出来也是一个Object的类型 定义的时候规定的类型是Integer,所以这里setName设置的时候 ...