在路由前置守卫获取存储用户信息

This commit is contained in:
jqtmviyu@gmail.com 2021-01-15 18:18:30 +08:00
parent 760d1596c8
commit 8d91031581
2 changed files with 6 additions and 2 deletions

View File

@ -75,13 +75,17 @@ import 'nprogress/nprogress.css'
const whiteList = ['/login', '/404']
// 全局前置路由守卫
router.beforeEach((to, from, next) => {
router.beforeEach(async(to, from, next) => {
NProgress.start()
// 判断是否有token
if (store.getters.token) { // 有token
if (to.path === '/login') { // 在登录页
next('/')
} else {
// 有token, 不在登录页, 会用到用户信息, 可以开始获取
if (!store.getters.userId) {
await store.dispatch('user/getUserInfo')
}
next()
}
} else { // 没有token

View File

@ -19,7 +19,7 @@ export default {
// getUserInfo().then(res => {
// console.log(res)
// })
this.$store.dispatch('user/getUserInfo')
// this.$store.dispatch('user/getUserInfo')
}
}
</script>