javascript - 嵌套路由未按预期工作 - React 路由器
问题描述
我的代码是
<Switch>
<Route path='/' exact render={() => <Redirect to='/route' />} />
<Route path='/route/' exact render={() => <Component />} />
<Route path='/route/subroute/:id+' exact render={() => <Component1/>} />
<Route path='/route/subroute/subsubroute1/:id+' exact render={() => <Component2 />} />
<Route path='/route/subroute/subsubroute2/:id+' exact render={() => <Component3 />} />
<Route path='/route/:subroute+' exact render={() => <Component4 />} />
<Route render={() => <Redirect to='/notfound' />} />
</Switch>
现在当我击中history.push(/route/subroute/id)
它时它正在工作
但是当我尝试打开history.push(/route/subroute/subroute1/id)
它时,它并没有为我打开页面。我试图浏览文件,但一切似乎都已到位。我错过了什么。
解决方案
+
运算符 at使/route/subroute/:id+
您匹配路线,并带有 param 。/route/subroute/subsubroute1/123
Component1
subsubroute1/123
删除+
它,它应该可以按预期工作。
或者,如果您确实需要使用+
,则可以向下移动Component1
,并且Component2
将正确匹配,因为react-router-dom
匹配与给定路径匹配的第一个路由:
<Switch>
<Route path='/' exact render={() => <Redirect to='/route' />} />
<Route path='/route/' exact render={() => <Component />} />
<Route path='/route/subroute/subsubroute1/:id+' exact render={() => <Component2 />} />
<Route path='/route/subroute/subsubroute2/:id+' exact render={() => <Component3 />} />
<Route path='/route/subroute/:id+' exact render={() => <Component1/>} />
<Route path='/route/:subroute+' exact render={() => <Component4 />} />
<Route render={() => <Redirect to='/notfound' />} />
</Switch>
推荐阅读
- javascript - 如何使用formData使用ajax从2个不同的文件输入中上传文件?
- java - JSPG0036E: 找不到资源 jsp
- php - 如何在给定数组所有序列号 1 到 n 的情况下创建输出 true 或 false 的 php 函数
- mysql - 在 Mysql 过程中似乎找不到错误 Error 1064 (42000)
- javascript - setTimeout 函数中的第三个参数是什么?
- android - 可以在 Application 类中调用 Web 服务吗?
- apache-kafka - 如何在正在运行的集群中为 ACL 创建 Kafka 用户和使用者组?
- crashlytics - Firebase crashlytics 错误加载事件趋势
- python-3.x - 有没有办法将最后一列第一个输出值值作为输入在数据框中的第一列第二个数据中使用其他列数据计算
- mongodb - 如何检查集合是 mongodb 中具有相同顺序的子集