【问题标题】:React Router Error - Cannot GET [Page Name]反应路由器错误 - 无法获取 [页面名称]
【发布时间】:2016-03-25 07:45:11
【问题描述】:

当我深入研究 React 时,我发现需要路由器。我正在关注 Wes Bos 的视频系列,但是当我尝试访问除我的根目录之外的任何路由时遇到了障碍。

import ReactRouter from 'react-router';
var Router = require('react-router').Router;
var Route = require('react-router').Route;
var Link = require('react-router').Link;
var createBrowserHistory = require('history/lib/createBrowserHistory');

然后是我的路由器

var routes = (
  <Router history={createBrowserHistory()}>
    <Route path="/" component={LoginPage} />
    <Route path="/dashboard" component={Dashboard} />
    <Route path="/search" component={SearchPage} />
    <Route path="/details" component={DetailsPage} />
    <Route path="*" component={LoginPage} />
  </Router>
);
ReactDOM.render(routes, document.getElementById('content'));

但是,如果我去 localhost:3000/dashboard 我会得到错误:

Cannot GET /dashboard

有什么建议吗?

【问题讨论】:

  • 您的后端服务器是如何设置的?使用浏览器历史记录,您还必须更改后端路由。

标签: reactjs react-router


【解决方案1】:

将此添加到 webpack.config.js

  devServer: {
      historyApiFallback: true
  }

【讨论】:

  • 这就是答案。 @Yuschick,您应该接受它以帮助查看此问题的人们快速确定解决方案。
  • 对我来说没有用。现在我收到错误:Cannot GET /
  • 感谢您的回答!它帮助了我。
  • @AbhaySood 从网站 (webpack.github.io/docs/webpack-dev-server.html) 中找到了这个:historyApiFallback: false, // Set this as true if you want to access dev server from arbitrary url. // This is handy if you are using a html5 router.
【解决方案2】:

您需要在使用浏览器历史记录时适当地配置您的服务器:https://github.com/rackt/react-router/blob/v1.0.3/docs/guides/basics/Histories.md#configuring-your-server

如果您使用的是 webpack 开发服务器,请启用 historyApiFallback

【讨论】:

  • "如果您使用的是 webpack 开发服务器,请启用 historyApiFallback。"这为我解决了这个问题。
猜你喜欢
  • 2022-11-20
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
  • 2021-09-25
  • 2016-05-29
  • 2022-11-30
  • 1970-01-01
  • 2019-05-08
相关资源
最近更新 更多