Ant-design-pro: 🧐[问题]使用umi运行时配置patchRoutes方法调整路由时未能加载页面组件

Created on 21 Jan 2020  ·  9Comments  ·  Source: ant-design/ant-design-pro

🧐 问题描述

rt 运行后BisicLayout加载正常,但未能加载Welcome组件

💻 示例代码

新建src/app.js patchRoutes(routes)方法部分代码如下

routes[1].routes.unshift(`
`   {`
`      path: '/',`
`      component: `
`        dynamic({ `
`          component: () => import( './layouts/BasicLayout'),`
`        }),`
`      routes:[`
`        {`
`          path: '/',`
`          redirect: '/welcome',`
`        },`
`        {`
`         path: '/welcome',`
`          name: 'welcome',`
`          icon: 'smile',`
`          component:`
`            dynamic({`
`                component: () => import('./pages/Welcome'),`
`            }),`
`          // exact: true,` 
`        },`
`      ]`
`    }`
`  );`

config.ts中配置,

`{ `
`path: '/',`
`component: '../layouts/SecurityLayout',`
`routes: [`   
`{`
`component: './404',`
`},`
`],`
`},

🚑 其他信息


antd-issue

🕵🏻‍♀️ question

All 9 comments

没有听懂你的描述,你是希望可以动态修改路由吗?

https://prolayout.ant.design/

感谢回复:  目前路由是在config.ts中的 routes: 字段下配置的一个数组,领导要求不在config中配置,想动态引入,也算是动态修改路由

------------------ 原始邮件 ------------------
发件人: "陈帅"<[email protected]>;
发送时间: 2020年1月28日(星期二) 晚上6:29
收件人: "ant-design/ant-design-pro"<[email protected]>;
抄送: " "<[email protected]>; "Author"<[email protected]>;
主题: Re: [ant-design/ant-design-pro] 類[问题]使用umi运行时配置patchRoutes方法调整路由时未能加载页面组件 (#5909)

没有听懂你的描述,你是希望可以动态修改路由吗?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

路由是不能动态修改的,你最好修改菜单,路由决定了你会打包什么文件进去,如果你再路由配置中不包含,js都不会直接打入。

路由是不能动态修改的,你最好修改菜单,路由决定了你会打包什么文件进去,如果你再路由配置中不包含,js都不会直接打入。

有办法实现题主说的功能吗?我也有类似的需求,config.ts 里不配置一些路由,运行时动态添加路由。打包时包含所有文件是能接受的。

@jing-wu 你要的是动态菜单吗

@chenshuai2144 不是动态菜单。
目前的需求是前端拿到后端给的页面层级结构(包含 path 和 component),前端生成相应的路由,然后展示。层级结构是变化的,所以没法在 config.ts 里事先配置好。

@jing-wu 按照我上面贴的示例代码,你需要在 config.ts 中配置 layout层,然后可以动态向 layout层中的route属性内添加页面路由.但是不能动态的添加layout层

@jing-wu 按照我上面贴的示例代码,你需要在 config.ts 中配置 layout层,然后可以动态向 layout层中的route属性内添加页面路由.但是不能动态的添加layout层

多谢!我尝试下。

另外,你提的这个问题现在解决了吗?Welcome组件没有加载,是因为没有事先在本地配置路由,所以打包时这个组件没有被打入,找不到组件导致的吗?

@Leon-JiangLe 有一个很笨的方法是,在routes配置中设置一个临时挂载点,挂载所有的路由,作用是加载l组件和model,待服务器获取菜单后,根据key取出挂载点的路由,修改后使用patchRoutes即可

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lvzheng0404 picture lvzheng0404  ·  3Comments

RichardStark picture RichardStark  ·  3Comments

happier2 picture happier2  ·  3Comments

Jerry-goodboy picture Jerry-goodboy  ·  3Comments

ghost picture ghost  ·  3Comments