Skip to content

Commit ff0b953

Browse files
committed
fix: 初始化侧边栏的时候返回的Promise有问题,进行了处理
1 parent 571ba78 commit ff0b953

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ const router = {
113113
mutations: {
114114
SET_MENUS (state, { authInfo, menus }) {
115115
localStorage.setItem('authInfo', JSON.stringify(authInfo));
116-
localStorage.setItem('menus', JSON.stringify(menus));
117116
state.menus = menus;
118117
state.authInfo = authInfo;
119118
}

src/router/permission.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@ router.beforeEach((to, from, next) => {
3333
if (to.path !== '/login' && to.path !== '/register') {
3434
const userInfo = JSON.parse(localStorage.getItem('userInfo'));
3535
if (userInfo) {
36-
store.commit('user/CHANGE_USER_INFO', userInfo);
36+
const { hasGetUserInfo } = store.state.user;
37+
if (!hasGetUserInfo) {
38+
store.commit('user/CHANGE_USER_INFO', userInfo);
39+
}
3740
const { hasGetRouter } = store.state.router;
3841
if (hasGetRouter) {
3942
authRouter(to, next);
4043
} else {
41-
initMenus().then(() => authRouter(to, next));
44+
initMenus().then(
45+
() => authRouter(to, next),
46+
() => next()
47+
);
4248
}
4349
} else {
4450
vm.$message.warning('请先登录后再访问');

src/store/modules/user.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import vm from '@/main';
44
const user = {
55
namespaced: true,
66
state: {
7-
userInfo: {}
7+
userInfo: {},
8+
hasGetUserInfo: false
89
},
910
//同步更改state中的状态
1011
mutations: {
1112
CHANGE_USER_INFO (state, userInfo) {
1213
localStorage.setItem('userInfo', JSON.stringify(userInfo));
1314
state.userInfo = userInfo;
15+
state.hasGetUserInfo = true;
1416
}
1517
},
1618
//异步更改state中的状态

src/utils/user.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ export const initMenus = () => {
2626
const authInfo = JSON.parse(localStorage.getItem('authInfo'));
2727
if (!authInfo) {
2828
// 发请求的时候要有用户信息
29-
return store.dispatch('router/GET_MENUS');
29+
store.dispatch('router/GET_MENUS').then(
30+
res => resolve(res),
31+
err => reject(err)
32+
);
3033
}
3134
const copyMenus = JSON.parse(JSON.stringify(menus));
3235
const authMenus = getAuthMenus(copyMenus, authInfo);

0 commit comments

Comments
 (0)