如何使用 React-Router V4 转义路径内的颜色字符 (:)? [英] How do I escape the color character (:) inside the path, with React-Router V4?

查看:44
本文介绍了如何使用 React-Router V4 转义路径内的颜色字符 (:)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的网址:

I have a urls that look something like this:

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

如您所见,我在 url 中有 :,需要考虑到这一点.我想要的是将组件 Home/foo/bar/main:home 匹配,但任何其他路径 /foo/bar/main:XXXXX> 匹配组件默认.

As you can see I have : in the url, that needs to be taken into consideration. What I want is to match component Home to /foo/bar/main:home, but any other path /foo/bar/main:XXXXX to match component Default.

到目前为止我尝试过的:

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

这一直匹配 Home 组件.

This matches the Home component all the time.

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

现在,默认值一直匹配.任何帮助,将不胜感激.谢谢.

Now, Default is matched all the time. Any help would be appreciated. Thanks.

推荐答案

React-router 使用 正则表达式路径.所以要转义 : 我认为这行得通:

React-router uses path-to-regexp. So to escape : I think this would work:

/foo/bar/main\\:contact_us

这篇关于如何使用 React-Router V4 转义路径内的颜色字符 (:)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆