可选的反应路由器参数 [英] Optional React Router parameter

查看:50
本文介绍了可选的反应路由器参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个匹配以下所有 URL 的路由:

I'm trying to create a route that matches all of the following URLs:

/product/foo
/product/foo/bar

这是我目前的路线:

<Route path="/product/:productName(/:urlID)" handler={SomeHandler} />

根据 https://github.com/rackt/react-router/blob/master/docs/guides/basics/RouteMatching.md 这个路由应该完美匹配,但它不匹配上面的任何一个 URL.

According to the documentation on https://github.com/rackt/react-router/blob/master/docs/guides/basics/RouteMatching.md this route should match perfectly but it does not match either of the URLs above.

我需要做什么来支持这个可选参数?

What do I need to do to support this optional parameter?

我使用的是 React Router 版本 0.13.3,如果我删除了 (/:urlID) 那么我可以匹配第一个 URL,但不能匹配第二个.

I'm on React Router version 0.13.3 and if I remove the (/:urlID) then I can match the first URL but not the second.

推荐答案

好的,所以 () 语法特定于 React Router 1.0,而不是 0.13.3.我最终使用了 ?语法:

Okay so the () syntax is specific to React Router 1.0, not 0.13.3. I ended up using the ? syntax:

<Route path="/product/:productName/?:urlID?" handler={SomeHandler} />

这篇关于可选的反应路由器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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