加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React Router中同一组件的多个路径名

发布时间:2020-12-15 20:54:21 所属栏目:百科 来源:网络整理
导读:我在三个不同的路线上使用相同的组件: Router Route path="/home" component={Home} / Route path="/users" component={Home} / Route path="/widgets" component={Home} //Router 无论如何要结合它,就像: Router Route path=["/home","/users","/widgets"
我在三个不同的路线上使用相同的组件:
<Router>
    <Route path="/home" component={Home} />
    <Route path="/users" component={Home} />
    <Route path="/widgets" component={Home} />
</Router>

无论如何要结合它,就像:

<Router>
    <Route path=["/home","/users","/widgets"] component={Home} />
</Router>
至少对于react-router v4,路径可以是正则表达式字符串,所以你可以这样做:
<Router>
    <Route path="/(home|users|widgets)/" component={Home} />
</Router>

你可以看到它有点冗长,所以如果你的组件/路线很简单,那么它可能不值得.

当然,如果这实际上经常出现,你总是可以创建一个包含数组路径参数的包装组件,它可以重复使用正则表达式或.map逻辑.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读