嵌套的React路由器4路由无法在Webpack 3

时间:2017-07-16 21:09:55

标签: webpack react-router webpack-dev-server html-webpack-plugin

因为这表明我无法获得像

这样的路线
<Route path="/events/:id" component={EventDetailRoute} />

工作,并且我读过的似乎是index.html中的捆绑包必须是绝对的,但是我使用HtmlWebpackPlugin 以便捆绑注入作为相对路径。

我已尝试为webpack设置输出配置,如下所示:

output: {
    path: path.resolve('dist'),
    filename: 'index_bundle.js',
    publicPath: '/'
},

但那也不起作用。

如果我尝试这条路线: http://localhost:8080/events/7 ,我在尝试查找 http://localhost:8080/events/index_bundle.js收到404错误

这是我的 webpack.config

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

const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './src/index.html',
    filename: 'index.html',
    inject: 'body'
})

module.exports = {
    entry: './src/index.js',
    output: {
        path: "/" + path.resolve('dist', 'build'),
        filename: 'index_bundle.js',
        publicPath: '/'
    },
    devServer: {
        historyApiFallback: true,
        hot: true,
        publicPath: '/'
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            camelCase: 'dashes',
                            localIdentName: '[name]__[local]'
                        }
                    },
                    {
                        loader: 'resolve-url-loader'
                    },
                    {
                        loader: 'sass-loader'
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [
                    { loader: 'style-loader' },
                    { loader: 'css-loader' },
                ]
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                use: {
                    loader: 'file-loader?name=[name].[ext]&outputPath=fonts/',
                }
            },
            {
                test: /\.(png|jpg)$/,
                use: {
                    loader: 'file-loader?name=[name].[ext]&outputPath=assets/',
                }
            }
        ]
    },
    plugins: [
        HtmlWebpackPluginConfig,
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NamedModulesPlugin()
    ]
}

我正在使用webpack 3.1.0,webpack-dev-server 2.5.1和react-router-dom 4.1.1

1 个答案:

答案 0 :(得分:15)

<base href="/" />添加到index.html文件的head标记

<head>
    <meta charset="utf-8">
    <title>React App Setup</title>
    <base href="/" />
</head>