Skip to content

Commit c258001

Browse files
committed
fix: 修复动态路由 不能正确加载
1 parent 8dcd439 commit c258001

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/features/router/initRouter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { isStaticSuper, selectUserInfo } from '../auth/authStore';
77

88
import { filterAuthRoutesByDynamic, filterAuthRoutesByRoles, mergeValuesByParent } from './shared';
99

10-
const hasRoutes = ['/manage/user', '/manage/user/:id', '/home', '/about'];
10+
const hasRoutes = ['/manage', '/manage/user', '/manage/user/:id', '/home', '/about'];
1111

1212
export function initAuthRoutes(addRoutes: (parent: string | null, route: RouteObject[]) => void) {
1313
const authRouteMode = import.meta.env.VITE_AUTH_ROUTE_MODE;

src/pages/layout.tsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function handleRouteSwitch(to: Router.Route, from: Router.Route | null) {
1212
if (to.handle.href) {
1313
window.open(to.handle.href, '_blank');
1414

15-
return { path: from?.fullPath, replace: true };
15+
return { path: from?.fullPath as string, replace: true };
1616
}
1717

1818
return null;
@@ -80,6 +80,8 @@ const RootLayout = () => {
8080

8181
const routeId = useRef<string>(null);
8282

83+
const location = useRef<string | { path: string; replace: boolean } | null>(null);
84+
8385
const { i18nKey, title } = handle;
8486

8587
const { roles } = useAppSelector(selectUserInfo);
@@ -99,25 +101,22 @@ const RootLayout = () => {
99101
if (routeId.current !== id) {
100102
routeId.current = id;
101103

102-
const location = createRouteGuard(route, roles, isSuper);
103-
104-
if (location) {
105-
if (typeof location === 'string') {
106-
return <Navigate to={location} />;
107-
}
108-
109-
if (location.path) {
110-
return (
111-
<Navigate
112-
replace={location.replace}
113-
to={location.path}
114-
/>
115-
);
116-
}
117-
}
104+
location.current = createRouteGuard(route, roles, isSuper);
118105
}
119106

120-
return <Outlet />;
107+
// eslint-disable-next-line no-nested-ternary
108+
return location.current ? (
109+
typeof location.current === 'string' ? (
110+
<Navigate to={location.current} />
111+
) : (
112+
<Navigate
113+
replace={location.current.replace}
114+
to={location.current.path}
115+
/>
116+
)
117+
) : (
118+
<Outlet />
119+
);
121120
};
122121

123122
export const loader = () => {

0 commit comments

Comments
 (0)