我有以下代码,我将处理嵌套路由,而输入错误的路由:
<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
时,什么都没有呈现!我应该如何以某种方式处理这个问题?
localhost:3000/sample/something-wrong
由path="/sample/:example"
处理
localhost:3000/sample/example/something-wrong
将由path="/sample/example/:id"
处理
当您使用通配符时,除了启用Example
组件以呈现适当的消息,或者可能重定向到专用的404屏幕之外,您无能为力。