React Router 4:如果页面与我的路由URL不匹配,则将页面路由到404页面

时间:2018-07-13 08:32:52

标签: reactjs react-router-v4 react-router-dom

<Switch>
<Route exact path="/" component={App}/>
<Route path="/home" component={Dashboard}/>
<Route component={NotFound}/>
</Switch>

当我的网址是http://localhost:8080/home1时,我将重定向到404页面,但是当我用子路径(http://localhost:8080/home/1给出了错误的网址时,它无法呈现我的NotFound组件并显示空屏幕。有人可以建议我我所缺少的吗?

1 个答案:

答案 0 :(得分:1)

<Switch>
    <Route exact path="/" component={App}/>
    <Route path="/home" component={Dashboard}/>
    <Route path="*" component={NotFound}/>
</Switch>