React Router无法正常工作

时间:2017-06-08 04:32:12

标签: reactjs react-router react-redux router

我是新手做出反应,并且不知道为什么这不起作用。

我的路由器代码在这里:

import React from 'react';  
import { Route, IndexRoute } from 'react-router'; 

import App from './components/App';  
import HomePage from './components/home/HomePage';  

export default (
  <Route path="/" component={App}>
    <IndexRoute component={HomePage} />
    <Route path="example" component={HomePage} />
  </Route>
);

这段代码会发生什么:

localhost /显示正常

localhost / example不显示

显示主页上标题的 localhost / example链接,但按下刷新不会

感谢您的帮助,我很感激!

这是我的webpack配置:

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  context: path.join(__dirname, "/client"),
  entry: [
    'webpack-dev-server/client?http://127.0.0.0:5000/',
    'webpack/hot/only-dev-server',
    'react-hot-loader/patch',
    './src/index.js'
  ],
  output: {
    path: path.join(__dirname, "/client/dist"),
    filename: '[name].js',
    publicPath: '/'
  },
  resolve: {
    extensions: ['.js', '.jsx', '.json']
  },
  devServer: {
    historyApiFallback: true
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: [ 'babel-loader', 'react-hot-loader/webpack' ],
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './static/template.html',
      inject: 'body',
      filename: 'index.html'
    }),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('development')
    })
  ]
};

app.js片段:

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(express.static(__dirname + 'client/dist'));
app.use(webpackDevMiddleware(compiler, {
    publicPath: webpackConfig.output.publicPath,
    hot: true,
    historyApiFallback: true,
    stats: {
        colors: true
    }
}));

我正在使用localhost:5000。链接的问题没有解决我的问题

1 个答案:

答案 0 :(得分:-1)

仅供参考,从版本4.0开始,React Router不再使用IndexRoute。

另外,对于您的路径,请将"example"更改为"/example"