React-router v4 - 无法获取 *url*

IT技术 javascript reactjs routes localhost react-router
2021-03-23 07:53:04

我开始使用 react-router v4。<Router>我的 app.js 中有一个简单的导航链接(见下面的代码)。如果我导航到localhost/vocabulary,路由器会将我重定向到正确的页面。但是,当我之后按重新加载 (F5) ( localhost/vocabulary) 时,所有内容都会消失并且浏览器报告Cannot GET /vocabulary这怎么可能?有人可以告诉我如何解决这个问题的任何线索(正确重新加载页面)?

应用程序.js:

import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
import { Switch, Redirect } from 'react-router'
import Login from './pages/Login'
import Vocabulary from './pages/Vocabulary'

const appContainer = document.getElementById('app')

ReactDOM.render(
  <Router>
    <div>
        <ul>
          <li><Link to="/">Home</Link></li>
          <li><Link to="/vocabulary">Vocabulary</Link></li>
        </ul>
        <Switch>
          <Route exact path="/" component={Login} />
          <Route exact path="/vocabulary" component={Vocabulary} />
        </Switch>
    </div>
  </Router>,
appContainer)
6个回答

我假设您正在使用 Webpack。如果是这样,在您的 webpack 配置中添加一些内容应该可以解决问题。具体来说,output.publicPath = '/'devServer.historyApiFallback = true这是下面的示例 webpack 配置,它同时使用 ^ 并为我修复了刷新问题。如果你好奇“为什么”,会有所帮助。

var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './app/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index_bundle.js',
    publicPath: '/'
  },
  module: {
    rules: [
      { test: /\.(js)$/, use: 'babel-loader' },
      { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]}
    ]
  },
  devServer: {
    historyApiFallback: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'app/index.html'
    })
  ]
};

我在这里写了更多关于这个 -在使用 React Router 刷新时修复“无法获取 /URL”错误(或客户端路由器如何工作)

由添加的 devServer 工作:{ historyApiFallback: true, },
2021-05-22 07:53:04
它适用于 devServer。但在构建后它不起作用:(
2021-05-23 07:53:04
并且不要使用带有 index.html 的 HtmlWebpackPlugin 作为模板,因为在路由刷新时不会注入包。将 bundle.js 脚本作为脚本标签直接插入到 index.html 中。
2021-05-27 07:53:04
来自我的 +1 并且每次更改 webpack.config.js 时不要忘记重新启动服务器(如果您启用了热加载)。
2021-06-11 07:53:04
它对我有用,只是添加了devServer { historyApiFallback: true } 我已经 publicPath: '/'配置好的
2021-06-18 07:53:04

只是为了补充泰勒对任何仍在为此苦苦挣扎的人的回答:

添加devServer.historyApiFallback: true到我的 webpack 配置(没有设置publicPath)修复了我在刷新/返回/转发时看到的 404/Cannot-GET 错误,但仅适用于单个级别的嵌套路由。换句话说,“/”和“/topics”开始工作正常,但除此之外的任何内容(例如“/topics/whatever”)仍然在刷新/等时抛出404。

刚刚在这里遇到了接受的答案:Unexpected token < error in react router component它为我提供了最后一个缺失的部分。/在我的包脚本 src 中添加引导index.html已经完全解决了这个问题。

它对我有用devServer { historyApiFallback: true }只需要添加就可以了,不需要使用publicPath: '/'

用法如:

  devServer: {
    historyApiFallback: true
  },

如果您的应用程序托管在静态文件服务器上,则需要使用 .

import { HashRouter } from 'react-router-dom'

ReactDOM.render((
  <HashRouter>
    <App />
  </HashRouter>
), holder)

https://github.com/ReactTraining/react-router/blob/v4.1.1/FAQ.md#why-doesnt-my-application-render-after-refreshing

您引用的文章真的很有帮助,因为我使用的是 express.js。这篇文章中的代码为我提供了接受除“/”之外的不同 URL 的提示和解决方案,因为它有一个星号作为 get() 函数的参数。app.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, 'index.html')) })
2021-05-28 07:53:04

我遇到了同样的问题,为我解决的问题是编辑我的package.json文件,然后在scripts: {

"build": "webpack -d && copy src\\index.html dist\\index.html /y && webpack-dev-server --content-base src --inline --port 3000"

在我的 webpackbuild代码的末尾,我添加了--history-api-fallback 这似乎也是最简单的Cannot GET /url错误解决方案

注意:下次在添加后构建时,--history-api-fallback您会注意到输出中的一行看起来像这样(无论您的索引文件是什么):

404s 将回退到 /index.html