SPA项目中,404页面 和 登陆页面 对应的路由,应该怎样控制?

  • SPA项目中,404页面 和 登陆页面 对应的路由,应该怎样控制?
    可以这样做:
  1. 登陆之前,所有页面跳到 登陆页面;包括随便输入的路由地址。
  2. 登陆后,跳到相应页面;随便输入的、不存在的路由地址,才跳到404页面。
  • react项目代码示例:
  1. 路由是login或其他:
    https://github.com/cag2050/react_eject_antd_demo/blob/master/src/index.js
    switch 是只能选择一个
<Switch>
  <Route exact path='/login' component={Login}/>
  <Route component={App}/>
</Switch>
  1. 路由是要进入的页面或找不到:
    https://github.com/cag2050/react_eject_antd_demo/blob/master/src/components/MyLayout.js
    switch 是只能选择一个
<Switch>
  {router.map((route, i) => <PrivateRoute key={i} exact={!!route.exact} path={route.path} component={route.component}/>)}
  <PrivateRoute component={NotFound}/>
</Switch>
原文地址:https://www.cnblogs.com/cag2050/p/8821812.html