React Router 4 - 处理404嵌套路由

问题描述 投票:0回答:1

我有以下代码,我将处理嵌套路由,而输入错误的路由:

<Switch>
<Route path="/" exact component={Home} />
<Route path="/sample" exact component={Sample} />
<Route path="/sample/example/:id" exact component={Example} />
<Route path="/sample/:example" exact component={Example} />
<Route component={404}/>
</Switch>

如果用户输入localhost:3000/something-wrong,它将正确显示404页面。但是,当他进入localhost:3000/sample/something-wrong时,什么都没有呈现!我应该如何以某种方式处理这个问题?

reactjs routes react-router-v4 nested-routes
1个回答
0
投票

localhost:3000/sample/something-wrongpath="/sample/:example"处理

localhost:3000/sample/example/something-wrong将由path="/sample/example/:id"处理

当您使用通配符时,除了启用Example组件以呈现适当的消息,或者可能重定向到专用的404屏幕之外,您无能为力。

© www.soinside.com 2019 - 2024. All rights reserved.