【问题标题】:Handling unspecified routes in react-router在 react-router 中处理未指定的路由
【发布时间】:2016-10-22 11:35:51
【问题描述】:

我有一个使用 React 路由器的 Express/React 应用程序,但我不确定如何处理来自 React 端的未指定路由。

我的 express 配置调用 app.get('/*', handler),它会渲染 index.html,以便将路由职责传递给 React。如何使用 React Router 处理尚未预先确定的路由。例如,如果我到达myapp.com/test,则会收到以下错误:

位置“/test”没有匹配任何路线

如何配置 react-router 以提供 404 页面,或在 URL 无法识别时重定向到默认路由?

【问题讨论】:

    标签: express reactjs react-router


    【解决方案1】:

    React Router 也有类似的指令:

    <Route path="*" component={NotFound} status={404} />
    

    这将渲染组件 NotFound 并确保将 404 状态发送到客户端,以防您需要它用于服务器端渲染 - 通用/同构应用程序。

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 2015-03-16
      • 2017-04-27
      相关资源
      最近更新 更多