允许在 React 的路由参数中使用斜杠 [英] Allow slashes in route parameter on React
问题描述
我有一个这样的路由器:
I have a router like this:
<Route path="/blog/:date/:folder" render={(props: any) => <Home />
它适用于此:
http://localhost/blog/2017-05-20/test
但是,:folder
可以有斜杠(子目录),我想一次解析文件夹中的所有路径.
However, :folder
can have slashes (sub-dir) and i want to parse all path at once in folder.
这不起作用:
http://localhost/blog/2017-05-20/test/sub/dir
在这种情况下,我只得到test
.我想将 :folder
作为 test/sub/dir
作为一个整体.
In this situation, I only get test
. I want to get :folder
as test/sub/dir
as a whole.
有什么办法可以通过 React Router 实现这一点吗?
Is there any way to achieve this with React Router?
预期:
:date => '2017-05-20'
:folder => 'test/sub/dir'
实际:
:date => '2017-05-20'
:folder => 'test'
推荐答案
是的,您可以通过向 Route 路径添加 +
来实现此目的.所以,像这样:
Yes, you can do this by adding a +
to your Route path. So, like this:
<Route path="/blog/:date/:folder+" ... />
这个库用于匹配路径.因此,对于更高级的匹配案例,这是一个更好的地方,然后查看 React Router 文档.
This library is being used for matching the path. So for more advanced matching cases this is a better place to look at then the React Router docs.
这篇关于允许在 React 的路由参数中使用斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!