reactjs - React Router: How to match indeterminate amount of sub paths of a nested route?
问题描述
I'm trying to create a file explorer like Dropbox. There will be an indeterminate amount of subpaths that look like this:
localhost:3001:file-manager/folder1/folder2/...folderN
I have this:
<Route path="/file-manager/:folderName" component={LandingPage} />
This allows me to visit: http://localhost:3001/file-manager/foo/bar/baz. However, when I check props.match
I don't see it, it only shows a match for foo
, not the levels below that (bar and baz).
match:
isExact: false
params: {folderName: "foo"}
path: "/file-manager/foo"
url: "/file-manager/foo"
How would I do this? All the tutorials I'm reading and watching about recursive routes append to the view. I want to replace the view.
解决方案
几个月前我遇到了完全相同的问题。我目前找不到我使用的确切代码,但如果我没记错的话,您可以使用星号来表示“从现在开始的所有内容”,其中包括任何进一步的路径分隔符:
<Route path="/file-manager/:folderName*" component={LandingPage} />
这确实意味着这props.match.params.folderName
将是完整的字符串,因此/
如果您需要一组路径组件,则必须自己拆分它。
重要提示:*
将使它匹配任何东西,所以如果你有一个你想要匹配的特定路线,请确保你把它放在这个之前,例如
<Route path="/file-manager/example" component={ExamplePage} />
<Route path="/file-manager/:folderName*" component={LandingPage} />
推荐阅读
- algorithm - 伪代码中的负数组索引
- netsuite - NetSuite:SuiteTalk 检索日记条目行
- java - 如何在依赖测试属于不同类的testng中保持顺序
- python - 为什么训练时准确率和损失保持不变?
- python - 如何在 Pyparsing 中编写字段定义,以免发生以下错误
- python - 使用 np.savetxt() 将多个数组保存在一个文件中
- javascript - 使用 javascript 从日期时间格式获取 cron 格式
- r - 填充按 ID 分组的间隔之间的值
- python - 如何更改散点矩阵中的配色方案?
- angular - 在 HTTP 拦截器中插入异步令牌