路线在重新加载时给出错误

时间:2016-06-16 19:46:28

标签: javascript reactjs redux react-router browser-history

所以我有使用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)。然后我尝试在其中一个视图/:id中重新加载页面,我收到错误:

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

你们有没有想过为什么会这样? 任何帮助将不胜感激。谢谢!

我从命令行运行webpack服务器:

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

1 个答案:

答案 0 :(得分:1)

确保您的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修复它。