路由守卫 判定token和白名单

This commit is contained in:
jqtmviyu@gmail.com 2021-01-15 16:33:38 +08:00
parent 7084b08dc9
commit 616ac68cc7
2 changed files with 27 additions and 1 deletions

View File

@ -62,3 +62,28 @@
// // finish progress bar // // finish progress bar
// NProgress.done() // NProgress.done()
// }) // })
// 路由守卫, 控制页面的访问权限
import router from '@/router'
import store from '@/store'
const whiteList = ['/login', '/404']
// 全局前置路由守卫
router.beforeEach((to, from, next) => {
// 判断是否有token
if (store.getters.token) { // 有token
if (to.path === '/login') { // 在登录页
next('/')
} else {
next()
}
} else { // 没有token
if (whiteList.indexOf(to.path) > -1) { // 在白名单
next()
} else {
next('/login')
console.log('aaa')
}
}
})

View File

@ -9,6 +9,7 @@
const getters = { const getters = {
sidebar: state => state.app.sidebar, sidebar: state => state.app.sidebar,
device: state => state.app.device device: state => state.app.device,
token: state => state.user.token
} }
export default getters export default getters