fix(projects): fix router when the dynamic routes api was failed [修复当动态路由接口失败后路由异常问题]

This commit is contained in:
Soybean
2022-11-17 22:54:45 +08:00
parent 3bd8858121
commit f2b580fc06
2 changed files with 17 additions and 8 deletions

View File

@ -25,6 +25,8 @@ interface RouteState {
authRouteMode: ImportMetaEnv['VITE_AUTH_ROUTE_MODE'];
/** 是否初始化了权限路由 */
isInitAuthRoute: boolean;
/** 动态路由是否初始化失败 */
failedInitDynamicRoute: boolean;
/** 路由首页name(前端静态路由时生效,后端动态路由该值会被后端返回的值覆盖) */
routeHomeName: AuthRoute.AllRouteKey;
/** 菜单 */
@ -39,6 +41,7 @@ export const useRouteStore = defineStore('route-store', {
state: (): RouteState => ({
authRouteMode: import.meta.env.VITE_AUTH_ROUTE_MODE,
isInitAuthRoute: false,
failedInitDynamicRoute: false,
routeHomeName: transformRoutePathToRouteName(import.meta.env.VITE_ROUTE_HOME_PATH),
menus: [],
searchMenus: [],
@ -112,11 +115,14 @@ export const useRouteStore = defineStore('route-store', {
throw new Error('userId 不能为空!');
}
const { data } = await fetchUserRoutes(userId);
if (data) {
const { error, data } = await fetchUserRoutes(userId);
if (!error) {
this.routeHomeName = data.home;
this.handleUpdateRootRedirect(data.home);
this.handleAuthRoute(data.routes);
} else {
this.failedInitDynamicRoute = true;
}
},
/** 初始化静态路由 */
@ -138,7 +144,7 @@ export const useRouteStore = defineStore('route-store', {
initHomeTab(this.routeHomeName, router);
this.isInitAuthRoute = true;
this.isInitAuthRoute = !this.failedInitDynamicRoute;
}
}
});