具有BrowserRouter/browserHistory的React-router在刷新时不起作用

Ale*_*dro 11 node.js reactjs webpack

我有以下webpack配置文件:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');

var config = {
    entry: [
        APP_DIR + '/config/routes.jsx',
        'webpack/hot/dev-server',
        'webpack-dev-server/client?http://localhost:8080'
    ],
  output: {
    publicPath: 'http://localhost:8080/src/client/public/'
  },
  module : {
    loaders : [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        include: APP_DIR,
        exclude: /node_modules/,
        query: {
            presets: ['es2015']
        }
      },
      {
        test: /\.scss$/,
        loaders: [ 'style', 'css', 'sass' ]
      }, 
      {
        test: /\.json$/, 
        loader: "json-loader"
     }
    ]
  }
};

module.exports = config;
Run Code Online (Sandbox Code Playgroud)

我想要做的就是在localhost上运行我的应用程序,但是当我点击时:" http:// localhost:8080/src/client/home "(根据我的routes.jsx和运行webpack-dev-server之后)

import React from 'react';

import { Route, Router, browserHistory } from 'react-router';
import ReactDOM from 'react-dom';

import Wrapper       from './../components/wrapper.jsx';
import Home          from './../components/home.jsx';
import Projects      from './../components/projects.jsx';
import SingleProject from './../components/projectContent/singleProject.jsx';
import About         from './../components/aboutUs.jsx'

ReactDOM.render((
    <Router history={browserHistory} >
        <Route path="/" component={Wrapper} >
            <Route path="home" component={Home} />
            <Route path="projects" component={Projects} />
            <Route path="projects/:id" component={SingleProject} />
            <Route path="about" component={About} />
        </Route>
    </Router>
), document.getElementById('app'));
Run Code Online (Sandbox Code Playgroud)

我明白了

"不能GET/src/client/home".

Shu*_*tri 15

您在路由中提到的第一件事是作为具有路径的主要组件/home.所以你需要访问http://localhost:8080/home.此外,如果您尝试直接访问此URL,它将在您使用时出现此错误browserHistory.如果你想要,你可以使用hashHistoryHashRouter在react-router v4中,在这种情况下你需要访问http://localhost:8080/#/home.如果你想继续使用browserHistoryBrowserRouter像在react-router v4中一样,那么你需要historyApiFallback: true 在webpack中加入

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');

var config = {
    entry: [
        APP_DIR + '/config/routes.jsx',
        'webpack/hot/dev-server',
        'webpack-dev-server/client?http://localhost:8080'
    ],
  output: {
    publicPath: 'http://localhost:8080/src/client/public/'
  },
  devServer: {
    historyApiFallback: true
  },
  module : {
    loaders : [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        include: APP_DIR,
        exclude: /node_modules/,
        query: {
            presets: ['es2015']
        }
      },
      {
        test: /\.scss$/,
        loaders: [ 'style', 'css', 'sass' ]
      }, 
      {
        test: /\.json$/, 
        loader: "json-loader"
     }
    ]
  }
};

module.exports = config;
Run Code Online (Sandbox Code Playgroud)


Bru*_* Mu 5

You need to add this in your webpack settings:

devServer: {
  historyApiFallback: true,
}, 
Run Code Online (Sandbox Code Playgroud)

And start your server like this:

webpack-dev-server --config webpack.config.js
Run Code Online (Sandbox Code Playgroud)

Because you want React-Route to handle the route instead of your server. So no matter what the url is it should goes to index.html.