Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

约定式路由模式下如何获取所有路由? #6188

Unanswered
shen774411223d asked this question in Q&A
Discussion options

使用约定式路由模式后,如何获取所有路由信息?在中后台中需要渲染路由菜单,现在是否提供了获取全部路由的api?
是否有import.meta.glob的平替?打包方式为Rspack

You must be logged in to vote

Replies: 4 comments 3 replies

Comment options

我的需求是这样,做个子应用模板,上手就写业务,根据路由生成菜单,嵌套主应用时只有路由部分的内容。
如果不要约定路由,这个框架也没必要用了。

之前看文档还是测试好像看到过有获取到约定下的所有路由,有点忘了,后面测试到再研究一下。

You must be logged in to vote
0 replies
Comment options

同样需要获取到全部路由,大佬们有什么好办法没有?

You must be logged in to vote
0 replies
Comment options

目前没有办法获取到,我们会在近期提供这个能力 @ibyxz @lanmingle @shen774411223d

You must be logged in to vote
1 reply
Comment options

目前没有办法获取到,我们会在近期提供这个能力 @ibyxz @lanmingle @shen774411223d

@zllkjc 那大概在那个版本提供该功能?

Comment options

#7130

目前已经合入主分支,本周发版,大家可以看看是否能满足需求。

You must be logged in to vote
2 replies
Comment options

@zllkjc 麻烦问一下这边为什么获取不到路由啊?

刚初始化的仓
image

import { useRuntimeContext } from '@modern-js/runtime';
import { useEffect } from 'react';
export default () => {
 const { routes } = useRuntimeContext();
 useEffect(() => {
 console.log(routes?.map(r => r.path));
 }, [routes]);
 return <h1>Profile</h1>;
};

output:
['/', '*']

expected:
e.g. ['/', 'profile', 'item', '*']

version:
"@modern-js/app-tools": "2.67.10"

Comment options

嗯,这里不是一个平铺的结构,是一个嵌套的结构。例如外层的 layout 其实是一层路由,而 item 和 profile 是下一层路由,这个结构是 React Router 吐出的,我们暂时没有将这个结构格式化或平铺。你可以在控制台输出它看一下,每一层都有一个 children 字段,可以自行遍历。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /