如何使用React-Router V4转义路径中的颜色字符(:)?

时间:2018-05-31 14:50:52

标签: reactjs react-router-v4

我的网址看起来像这样:

/foo/bar/main:home
/foo/bar/main:about_us
/foo/bar/main:contact_us

正如您所看到的,我在网址中有,这需要加以考虑。我想要的是将组件主页/foo/bar/main:home匹配,但将任何其他路径/foo/bar/main:XXXXX默认组件匹配。

到目前为止我尝试了什么:

<Route path={'/foo/bar/main:home'} component={Home} />
<Route path={'/foo/bar/main:any'} component={Default} />

这始终与Home组件匹配。

<Route path={'/foo/bar/main%3Ahome'} component={Home} />
<Route path={'/foo/bar/main:any'} component={Default} />

现在,Default始终匹配。任何帮助,将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

React-router使用path-to-regexp。所以为了逃避:,我认为这样可行:

/foo/bar/main\\:contact_us