用嵌套路由反应路由器 4 "Not Found" 页面

react router 4 "Not Found" page with nested routes

假设我有这些路线:

   <Switch>
    <Route path="/my-profile" component={MyProfile} />
    <Route path="/logout" component={Logout} />
    <Route component={NotFound} />
   </Switch>

如果我在地址栏中键入 my-profile 以外的任何内容或注销,则处理 404 页面工作正常。但是,如果我输入 .../my-profile/paththatdoesntexist,我仍然会看到个人资料页面。如何处理此类情况?无论路径嵌套多少,都显示未找到的页面。

您可以在 react-router-4 中使用 exact 道具,如下所示。

<Switch>
  <Route exact path="/my-profile" component={MyProfile} />
  <Route exact path="/logout" component={Logout} />
  <Route component={NotFound} />
 </Switch>

如需更多参考,您可以在此处阅读 exact: bool