首页 > 解决方案 > BrowserRouter 未使用 url 参数打开

问题描述

我有以下路由器配置:

const App = () => (
  <HashRouter>
    <div className="app">
      <Switch>
        <Route exact path="/" component={Landing} />
        <Route exact path="/search" component={Search} />
        <Route path="/details/:id" component={Details} />
        <Route component={FourOhFour} />
      </Switch>
    </div>
  </HashRouter>
)

当我加载http://localhost:8080/#/details/12345我的组件详细信息时,会正确呈现;
如果我切换到 BrowserRouter,加载http://localhost:8080/details/12345,我会得到一个空白页面: index.html 正确呈现,但我在控制台中收到以下错误:

拒绝应用来自 ' http://localhost:8080/details/public/style.css ' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查。

GET http://localhost:8080/details/public/bundle.js 404(未找到)1:1

拒绝执行来自
' http://localhost:8080/details/public/bundle.js ' 的脚本,因为它的 MIME 类型 ('text/html') 不可执行,并且启用了严格的 MIME 类型检查。

我正在学习在线课程,我正在使用 webpack,我认为我的所有配置都与课程中的配置相同,课程视频中的应用程序可以正常工作

我的 webpack.config.js 如下:

const path = require('path')
const webpack = require('webpack')

module.exports = {
  context: __dirname,
  entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './js/ClientApp.jsx'
  ],
  devtool: process.env.NODE_ENV === 'development' ? 'cheap-eval-source-map' : false,
  output: {
    path: path.resolve(__dirname, 'public'),
    filename: 'bundle.js',
    publicPath: 'public/'
  },
  devServer: {
    hot: true,
    publicPath: '/public/',
    historyApiFallback: true
  },
  resolve: {
    extensions: ['.js', '.jsx', '.json']
  },
  stats: {
    colors: true,
    reasons: true,
    chunks: true
  },
  plugins: [new webpack.HotModuleReplacementPlugin(), new webpack.NamedModulesPlugin()],
  module: {
    rules: [
      // {
      //   enforce: 'pre',
      //   test: /\.jsx?$/,
      //   loader: 'eslint-loader',
      //   exclude: /node_modules/
      // },
      {
        test: /\.jsx?$/,
        loader: 'babel-loader'
      }
    ]
  }
}

标签: reactjswebpack-2react-router-v4react-router-dom

解决方案


推荐阅读