react-router-dom 无匹配路由,跳转到404的问题


https://stackoverflow.com/que...
中有提到两种方案:

<Switch>
    <Route exact path="/users" component={MyComponent} />
    <Route component={GenericNotFound} />
</Switch>
<Switch>
    <Route path="/users" component={MyComponent} />
    <Route path="/404" component={GenericNotFound} />
    <Redirect to="/404" />
</Switch>

第一种方案在实践过程中发现,首页也匹配了404,其他页面正常;第二种方案中,所有页面都会重定向到404页面
个人路由设置:

<BrowserRouter>  
     <Switch> 
        <ScrollTop> 
            <Route path\='/' exact component\={Index}/>  
            <Route path\='/list' component\={ApiList} />  
            <Route path\='/404' component\={errorPage} />  
             <Route component\={errorPage} />  
        </ScrollTop> 
     </Switch>
 </BrowserRouter>

react-router-dom版本:4.4.0
请问是啥原因。

阅读 8.4k
2 个回答

你还是复习一下react-router吧

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题