【问题标题】:Can't use nested routes with React-Router and Webpack不能在 React-Router 和 Webpack 中使用嵌套路由
【发布时间】:2016-02-04 08:12:12
【问题描述】:

我需要一些关于如何使用带有浏览器历史记录和 Webpack 的 React-router 的帮助。一切正常,直到我需要使用嵌套路由时,我的 bundle.js 上出现 404。

没有历史记录(url 中带有#s)一切正常,但我宁愿不使用它们。我尽可能使用代理服务器(下面的代码),它确实解决了问题,但也意味着我似乎无法使用我需要的所有其他 Webpack 东西(如 postcss 等)。至少我不知道怎么做。

所以我真的很想让这个东西使用 Webpack 来处理我的 postcss、React-router 以及漂亮干净的 url 和嵌套路由。到目前为止看起来不太有希望......

这是我的 webpack 配置文件(es6 语法):

import path from 'path';
import HtmlwebpackPlugin from 'html-webpack-plugin';
import merge from 'webpack-merge';
import ExtractTextPlugin from 'extract-text-webpack-plugin';

import postcssImport from 'postcss-import';
import nested from 'postcss-nested';
import mqpacker from 'css-mqpacker';
import cssnext from 'postcss-cssnext';

const TARGET = process.env.npm_lifecycle_event;
const ROOT_PATH = path.resolve(__dirname);
const APP_PATH = path.resolve(ROOT_PATH, 'app');
const BUILD_PATH = path.resolve(ROOT_PATH, 'public');

const common = {
  entry: [
    APP_PATH + '/App.js',
  ],
  output: {
    path: BUILD_PATH,
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel',
      },
      {
        test: /\.css$/,
        /* loader: 'style-loader!css-loader!postcss-loader',*/
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!postcss-loader'),
      },
    ],
  },
  postcss: function(webpack) {
    return [
      postcssImport({
        addDependencyTo: webpack,
      }),
      nested,
      mqpacker,
      cssnext(),
    ];
  },
};

if (TARGET === 'dev' || !TARGET) {
  module.exports = merge(common, {
    devtool: 'inline-source-map',
    devServer: {
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,
    },
    plugins: [
      new ExtractTextPlugin('app.css'),
      new HtmlwebpackPlugin({
        title: 'Hohohohoho',
        template: BUILD_PATH + '/index.html',
        inject: 'body',
      }),
    ],
  });
}

...工作正常,但无法使用嵌套路由。这又是我在 node 上尝试过的东西并让它工作了,但是在 webpack 中使用 postcss 失败了:

require('babel/register')({});
var server = require('pushstate-server');

server.start({
  port: process.env.PORT || 8090,
  directory: './public'
});

var WebpackDevServer = require("webpack-dev-server");
var webpack = require("webpack");

var compiler = webpack(process.argv[2] == 'hot' ? require('./webpack.config.hot.js') : require('./webpack.config.babel.js'));
var devServer = new WebpackDevServer(compiler, {

  stats: {colors: true},
  contentBase: 'http://localhost:8090/',
  publicPath: 'http://localhost:8080/js/',

  hot: process.argv[2] == 'hot'
});

devServer.listen(8080, "localhost", function() {})

有人告诉我使用 historyApiFallback 但这似乎并没有改变任何东西,但它仍然存在于 webpack 选项中。

【问题讨论】:

    标签: reactjs webpack react-router


    【解决方案1】:

    您需要做的就是将history 参数添加到您的路由器:

    import React from 'react'
    import { render } from 'react-dom'
    import { Router } from 'react-router'
    import createBrowserHistory from 'history/lib/createBrowserHistory'
    
    const history = createBrowserHistory()
    
    render((
      <Router history={history}>
        {/* some routes */}
      </Router>
    ), document.getElementById('root'));
    

    有关createBrowserHistory() 的更多信息,请参阅文档。

    确保您已安装 history 模块:npm install history

    【讨论】:

    • 谢谢,但这正是我无法工作的地方。就像我写的那样,没有历史,一切都很好,但是我需要在我的 url 中有那些丑陋的#s。当我有嵌套路由时,历史记录会失败,example.com/blaah 工作得很好,但是 example.com/blaah/bleeh 不再工作了......
    • 告诉我你的路线,我会帮你的。同时你可以看看我的playground example。您可以查看 webpack 设置并进行操作。
    • 这与github.com/rackt/react-router/issues/2303 有关吗?如果是这样,我也无法弄清楚如何使用历史记录和渲染非根组件。文档提到我需要设置服务器(github.com/rackt/react-router/blob/v1.0.0-rc3/docs/guides/…),但他们没有告诉我该怎么做。他们提到快递,但不幸的是我对快递一无所知。现在我决定从 中删除 history = {createHistory()} 因为我只是不知道如何使用它。
    猜你喜欢
    • 2018-09-29
    • 2022-08-17
    • 1970-01-01
    • 2016-01-07
    • 2016-05-03
    • 2016-03-06
    • 2017-11-11
    • 1970-01-01
    相关资源
    最近更新 更多