【发布时间】:2018-05-02 07:33:45
【问题描述】:
我正在使用 React Router V4,并且我有这个路由器的配置:
<Router history={customHistory}>
<Switch>
<Route exact path={`/:lng?`} component={Page} />
<Route path={`/:lng?/firstUrl`} component={Page}/>
<Route path={`/:lng?/secondUrl`} component={Page}/>
<Route component={NoMatch} />
</Switch>
</Router>
lng 是可选的语言参数,应该匹配en、de 等模式或不存在。
例如应用程序使用这些路由:
www.website.com/en
www.website.com/en/
www.website.com/ - will load default lang
www.website.com - will load default lang
www.website.com/de
www.website.com/de/
我还有一个额外的组件,我在其中定义了函数的路由:
<ModalRoute component={SomeURL} path={`/:lng?/SomeURL`} parentPath={`/${lang}/`} />
<ModalRoute component={SomeURL2} path={`/:lng?/SomeURL2`} parentPath={`/${lang}/`} />
因此,我希望实现的结果是,只接受允许的语言代码(和空参数),而不允许的语言代码 - 将被重定向到 NoMatch 组件。
有可能吗?如何实现?
非常感谢示例
【问题讨论】:
-
路由模式匹配正在开发中:github.com/ReactTraining/react-router/pull/3105
-
距离上次讨论已经过去一年半了……不明白这个功能是在进行中还是被遗忘
标签: reactjs react-router react-router-v4