let path = require('path')
module.exports = {
    entry:path.resolve('public/src/index.js'),
    output: {
        path:__dirname + "/public",
        filename: "bundle.js"
    },
    module: {
        loaders: [{
            exclude: /(node_modules)/,
            loader:'babel-loader',
            query:{
                presets: ['es2015', 'react']
            }
        }]
    },
    watch:true
};





我正在使用reactjs路由器,在教程中,他们大多数都告诉你写

--history-api-fallback使路由器工作



let path = require('path')
module.exports = {
    entry:path.resolve('public/src/index.js'),
    output: {
        path:__dirname + "/public",
        filename: "bundle.js"
    },
    module: {
        loaders: [{
            exclude: /(node_modules)/,
            loader:'babel-loader',
            query:{
                presets: ['es2015', 'react']
            }
        }]
    },
    watch:true
};

{
  "name": "pr_v1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server/index.js & webpack",

    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.16.2",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "body-parser": "^1.18.2",
    "dotenv": "^4.0.0",
    "express": "^4.15.4",
    "moment": "^2.18.1",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-redux": "^5.0.6",
    "react-router": "^4.2.0",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0",
    "request": "^2.82.0",
    "webpack": "^3.5.5"
  }
}

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import Reducer from './Reducer/';
import {Router, Route, browserHistory} from 'react-router';

import App from './Components/App.jsx';







const store = createStore(Reducer, applyMiddleware(thunk));




ReactDOM.render(

     <Router history={browserHistory}>
         <Route path='/user' component={App}/>
     </Router>

    ,document.getElementById('root')); 





我是React Router的新手,我正在尝试使--history-api-fallback正常工作。教程说将其放入json文件的构建中,但我没有构建脚本。我认为也许应该在webpack配置文件中。我不确定。我的问题是我可以在哪里插入--history-api-fallback,以便它可以正常工作。

最佳答案

对于webpack开发服务器,您只需要添加webpack开发配置。这将在您的开发环境中工作。

devServer:
{
  historyApiFallback: true
},

10-07 17:34