Skip to content

Commit 4de6a93

Browse files
authored
Merge pull request #5 from epeejs/feature
fix: 修复根路径重定向一直跳转第一个叶节点问题
2 parents 54a3f1d + c71217a commit 4de6a93

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

packages/pro-layout/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@epeejs/pro-layout",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "用于快速搭建中后台页面结构",
55
"main": "es/index.js",
66
"module": "es/index.js",

packages/pro-layout/src/layouts/BasicLayout.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function BasicLayout<AuthorityType = any>({
8888
},
8989
{ siderRender: true, headerRender: true, footerRender: true },
9090
);
91-
const displayRoutes = useMemo(() => {
91+
const menuRoutes = useMemo(() => {
9292
const filterNode = (nodes: RouteConfig[]): RouteConfig[] => {
9393
return nodes
9494
.map((m) => ({ ...m }))
@@ -129,16 +129,21 @@ function BasicLayout<AuthorityType = any>({
129129
});
130130
};
131131
const newRoutes = wrapNode(routes);
132-
const firstLeafNode = getFirstLeafNode(displayRoutes);
133132
const redirectNode = newRoutes.find((m) => m.path === '/' && m.redirect);
134133

135-
if (firstLeafNode && redirectNode && redirectNode.redirect !== firstLeafNode.path) {
136-
redirectNode.redirect = firstLeafNode.path;
134+
if (redirectNode) {
135+
const targetRoute = getRouteConfigByPath(menuRoutes, redirectNode.redirect!);
136+
const firstLeafNode = getFirstLeafNode(menuRoutes);
137+
138+
// 如果重定向页面无权限访问且第一个叶节点存在,则修改为跳转到第一个叶节点
139+
if (!targetRoute && firstLeafNode) {
140+
redirectNode.redirect = firstLeafNode.path;
141+
}
137142
}
138143

139144
return newRoutes;
140145
// eslint-disable-next-line react-hooks/exhaustive-deps
141-
}, [authInfo, displayRoutes, routes]);
146+
}, [authInfo, menuRoutes, routes]);
142147
const getSider = () => {
143148
const menu = pathname !== '/' && (
144149
<Menu
@@ -156,7 +161,7 @@ function BasicLayout<AuthorityType = any>({
156161
}
157162
}}
158163
>
159-
{renderMenu(displayRoutes)}
164+
{renderMenu(menuRoutes)}
160165
</Menu>
161166
);
162167
return siderRender ? siderRender(menu) : <Sider>{menu}</Sider>;

packages/pro-layout/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function getRouteConfigByPath(routes: RouteConfig[], path: string): Route
4343
export function getFirstLeafNode(routes: RouteConfig[]): RouteConfig | undefined {
4444
const firstNode = routes[0];
4545

46+
// 不存在节点时,则返回 undefined
4647
if (!firstNode) {
4748
return firstNode;
4849
}

0 commit comments

Comments
 (0)