我和你一样,也和你一样失败。文件仍然是错误的。而且我发现从服务器获取菜单在 ant design pro v4 中有很多错误。(也许我不知道)所以我的最终决定是按照最初的设计显示 /config/config.ts 中的所有菜单。并仅从服务器获取授权信息并仅设置权限以仅显示登录用户相关菜单。所以我的解决方案(不正确的答案)是:
我引用了这个链接。https://umijs.org/docs/runtime-config#patchroutes-routes-
创建文件/src/app.tsx
并插入代码如下:
interface PathAndIdsModel {
path: string;
ids: string[];
}
const setAuthority = (routes: any, pathAndIds: PathAndIdsModel[]) => {
routes.forEach((route: any) => {
const found = pathAndIds.find((item) => item.path === route.path);
if (found) {
// eslint-disable-next-line no-param-reassign
route.authority = [...new Set([...(route.authority || []), ...found.ids])];
}
if (route.routes) {
setAuthority(route.routes, pathAndIds);
}
});
};
async function patchRoutes({ routes }) {
const response = await fetch(`https://localhost:44357/authorities`);
const pathAndIds = await response.json();
setAuthority(routes, pathAndIds);
}
export { patchRoutes };
将以下代码插入 ASP.Net 核心控制器:
[HttpGet("/authorities")]
public IEnumerable<object> Authorities()
{
return new[]
{
new {
Path = "/dashboard/analysis",
Ids = new [] { "user", "admin", },
},
new {
Path = "/dashboard/monitor",
Ids = new [] { "user", "admin", },
},
new {
Path = "/dashboard/workplace",
Ids = new [] { "admin", },
},
new {
Path = "/form/basic-form",
Ids = new [] { "admin", },
},
};
}
/dashboard/workplace
如果以用户身份登录,/form/basic-form
页面将被隐藏,但如果以管理员身份登录,则显示。
我试图从服务器获取完整路由,但由于异步调用而失败,UmiJS 没有等到从服务器获取并设置新路由。因此,当我从服务器获取路由并更改路由时,UmiJS 已经转换了旧路由的图标和组件,而我的新路由从未改变。