因此,我可以使用redux进行路由,但是在其中一条路由中重新加载时遇到了问题。

app.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute } from 'react-router';
import { Provider } from 'react-redux';
import store , { history } from './store';

import Connect from './components/Connect';
import PhotoList from './components/PhotoList';
import PhotoContent from './components/PhotoContent';

ReactDOM.render(
  <Provider store={store}>
    <Router history={history}>
      <Route path="/" component={Connect}>
        <IndexRoute component={PhotoList}></IndexRoute>
        <Route path="view/:id" component={PhotoContent}></Route>
      </Route>
    </Router>
  </Provider>,
  document.getElementById("app")
);


webpack.config.js:

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');

module.exports = {
  context: path.join(__dirname, "src"),
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./js/app.js",
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0'],
          plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
        }
      }
    ]
  },
  output: {
    path: __dirname + "/src/",
    filename: "app.min.js"
  },
  plugins: debug ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
  ],
};


这是我的代码,history = syncHistoryWithStore(store, browserHistory)。然后我尝试在其中一个view /:id内重新加载页面时,出现错误:

GET http://localhost:8080/view/app.min.js

你们知道为什么会这样吗?
任何帮助将不胜感激。谢谢!

我使用以下命令从命令行运行webpack服务器:

webpack-dev-server --content-base src --inline --hot --history-api-fallback

最佳答案

确保您的index.html引用了正确的捆绑文件。

对于webpack,请确保您的配置使用以下命令:

config.devServer = {
    ...
    contentBase: 'src',
    historyApiFallback: true,
    ...
};


或者,如果您将其声明为单个对象:

{
    ...
    devServer: {
       ...
       contentBase: 'src',
       historyApiFallback: true
    }
}


它将把您的请求重定向到/并做出反应来处理它。

像这样运行webpack-dev-server./node_modules/.bin/webpack-dev-server --inline --hot

编辑:

通过聊天与您合作时,我注意到您index.html中的捆绑包引用不正确。将其移动到../app.min.js可以修复它。

09-30 16:41
查看更多