React Router 中同一组件的多个路径名 [英] Multiple path names for a same component in React Router

查看:35
本文介绍了React Router 中同一组件的多个路径名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为三个不同的路由使用相同的组件:

I am using the same component for three different routes:

<Router>
    <Route path="/home" component={Home} />
    <Route path="/users" component={Home} />
    <Route path="/widgets" component={Home} />
</Router>

有没有办法把它结合起来,就像:

Is there anyway to combine it, to be like:

<Router>
    <Route path=["/home", "/users", "/widgets"] component={Home} />
</Router>

推荐答案

As of react-router v4.4.0-beta.4,正式在 v5.0.0 中,您现在可以指定解析为组件的路径数组,例如

As of react-router v4.4.0-beta.4, and officially in v5.0.0, you can now specify an array of paths which resolve to a component e.g.

<Router>
    <Route path={["/home", "/users", "/widgets"]} component={Home} />
</Router>

数组中的每个路径都是一个正则表达式字符串.

Each path in the array is a regular expression string.

可以在此处找到此方法的文档.

这篇关于React Router 中同一组件的多个路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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