Closed resuper closed 2 years ago
同问 app.jsx文件
import { dynamic } from '@umijs/runtime';
import LoadingComponent from '@ant-design/pro-layout/es/PageLoading';
let extraRoutes = [];
export function patchRoutes({ routes }) {
routes[1].routes = routes[1].routes.concat(extraRoutes);
// 如果最后页面是404,所以要插入到时候第二个
// routes[1].routes.splice(-1, 0, ...extraRoutes);
console.log(history.location.pathname, routes)
console.log(extraRoutes)
}
export function render(oldRender) {
// fetch('/api/routes').then(res=>res.json()).then((res) => {
extraRoutes = [{
path: '/welcome2',
name: 'welcome',
icon: 'smile',
// hideInMenu: true,
exact: true,
access: "canAdmin",
component: dynamic({ loader: () => import('./pages/Form'), loading: LoadingComponent}),
}];
oldRender();
// })
}
终于解决了!!!!!!
不推荐使用动态路由,动态菜单效果更好
@chenshuai2144 patchRoutes和childrenRender不兼容,用了patchRoutes后childrenRender不执行了
不推荐使用动态路由,动态菜单效果更好
请问后续会支持吗,我遇到了同样的问题~
不会支持的,不要玩这种花样
@chenshuai2144 为什么不推荐动态路由?
🧐 需求:菜单从数据库加载,路由也从数据库加载。
目前情况:目前菜单从数据库加载已经实现,目前菜单从数据库加载已经实现,但是菜单叶子节点中,每个页面打开都是404,只能访问配置好的静态路由对应的页面,原因:因为路由被删了,路由匹配不上导致404。
待大佬解答的问题:路由可否也从数据库加载?,route.js中原有的配置被我删除了,因为antdpro默认会用route.js生成菜单,所以我删除了
问题定位:缺失路由配置,参照umi官网动态加载路由,写法不对,umi官网写的动态修改路由的方法中require得到的对象不是antdpro中的组件,antdpro中是否有单独引入组件的方法???
问题分析过程:
routes.unshift({ path: '/foo', exact: true, component: require('@/extraRoutes/foo').default, });
require获得的对象不是antdPro中的组件对象,类型不一样(看结构感觉不是一个东西,我不是专业前端,有没说对的地方烦请大佬指正)前言万语其实都在下面这张图和下面的示例代码中。。
💻 示例代码
route.js
staticRoutesHighPriority.js
staticRoutesLowPriority.js
运行时配置,只贴出了实现动态加载菜单的部分 app.jsx