🐛 [BUG]我打包后从layout 配置的 服务器获取的菜单无法触发 ,我在开发模式跑是没有问题 #11414
Description
🐛 bug 描述
📷 复现步骤 | Recurrence steps
打包以后出现
🏞 期望结果 | Expected results
和dev 打包的效果一样
和dev 打包的效果一样
💻 复现代码 | Recurrence code
export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
useEffect(() => {
// 请求接口获取标题
const fetchTitle = async () => {
const response = await getSysShow();
if (response.success === true) {
const datainfo = response.data;
const data = datainfo[0];
flushSync(() => {
setInitialState((s) => ({
...s,
sysconfg: data,
}));
});
flushSync(() => {
setInitialState((s) => ({
...s,
currentUser: data,
}));
});
// 打印数据
window.console.log(initialState);
} else {
return {};
}
};
fetchTitle();
}, []);
useEffect(() => {
// 检查 initialState?.currentUser?.uid 是否存在
if (initialState?.currentUser?.uid) {
console.log('当前用户 UID:', initialState.currentUser.uid);
} else {
// 如果没有 uid, 也可以打印一个提示,表示 uid 还未存在
console.log('当前用户 UID 不存在');
}
}, [initialState?.currentUser?.uid]);
useEffect(() => {
// 检查 initialState?.currentUser?.uid 是否存在
if (initialState) {
console.log('当前用户历史数据:', initialState);
} else {
// 如果没有 uid, 也可以打印一个提示,表示 uid 还未存在
console.log('当前用户历史数据');
}
}, [initialState]);
return {
actionsRender: () => [, ],
locale: 'zh-CN', //关闭国际化
menu: {
// 每当 initialState?.currentUser?.userid 发生修改时重新执行 request
params: {
userId: initialState?.currentUser?.uid,
},
locale: false, //关闭国际化
// 获取菜单列表
request: async (params, defaultMenuData) => {
// initialState.currentUser 中包含了所有用户信息
try {
// 获取菜单中
console.log('获取菜单中');
const menuData = await getmenu({});
const defaultMenus = parseMenuData(menuData.data);
console.log(defaultMenus);
const Menus_list = loopMenuItem(defaultMenus);
console.log(Menus_list);
return Menus_list;
} catch (error) {
console.error('获取菜单数据失败:', error);
return []; // 在失败的情况下返回一个空数组,防止渲染出错
}
},
},
// layout 的左上角的 title
// title: initialState?.sysconfg?.system_title ?? '未定义',
title: 'nihao',
// logo: initialState?.sysconfg?.system_logo,
logo: ,
avatarProps: {
src: initialState?.currentUser?.enterprise_icon,
title: ,
render: (_, avatarChildren) => {
return {avatarChildren};
},
},
waterMarkProps: {
content: initialState?.currentUser?.name,
},
footerRender: () =>
onPageChange: () => {
const { location } = history;
// 如果没有登录,重定向到 login
if (!initialState?.currentUser && location.pathname !== loginPath) {
history.push(loginPath);
}
},
bgLayoutImgList: [
{
src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/D2LWSqNny4sAAAAAAAAAAAAAFl94AQBr',
left: 85,
bottom: 100,
height: '303px',
},
{
src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/C2TWRpJpiC0AAAAAAAAAAAAAFl94AQBr',
bottom: -68,
right: -45,
height: '303px',
},
{
src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/F6vSTbj8KpYAAAAAAAAAAAAAFl94AQBr',
bottom: 0,
left: 0,
width: '331px',
},
],
links: isDev
? [
OpenAPI 文档
,
]
: [],
menuHeaderRender: undefined,
// 自定义 403 页面
// unAccessible:
// 增加一个 loading 的状态
childrenRender: (children) => {
// if (initialState?.loading) return ;
return (
<>
{children}
{isDev && (
<SettingDrawer
disableUrlParams
enableDarkTheme
settings={initialState?.settings}
onSettingChange={(settings) => {
setInitialState((preInitialState) => ({
...preInitialState,
settings,
}));
}}
/>
)}
</>
);
},
...initialState?.settings,
};
};
© 版本信息
- Ant Design Pro 版本: [e.g. 6.0.0]
- umi 版本
- 浏览器环境
- 开发环境 [e.g. mac OS]