相关疑难解决方法(0)

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

由于小提示我不能得到像这样的路线

<Route path="/events/:id" component={EventDetailRoute} />
Run Code Online (Sandbox Code Playgroud)

工作,因为我读过似乎是index.html中的bundle必须是绝对的,但是我使用的是HtmlWebpackPlugin,所以bundle被注入一个相对路径.

我试图为webpack设置输出配置如下:

output: {
    path: path.resolve('dist'),
    filename: 'index_bundle.js',
    publicPath: '/'
},
Run Code Online (Sandbox Code Playgroud)

但这也不起作用.

如果我尝试这条路线: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: { …
Run Code Online (Sandbox Code Playgroud)

webpack react-router webpack-dev-server html-webpack-plugin

4
推荐指数
1
解决办法
2024
查看次数

HistoryApiFallback 不适用于 Webpack 4(使用 React Router 4 和嵌套路由)

上下文:使用 webpack 4 尝试 RR4 (devServer: {apiHistoryFallback: true} Webpack 4 配置示例

http://localhost:8080/reports在剪切并粘贴到地址栏时有效(又名历史回退正在工作),我可以单击 /reports/8 的链接,因为我已在 /reports 处添加了到组件的嵌套路由

<Route path=${match.path}/:reportId component={ReportDetail}/>

但是将http://localhost:8080/reports/9剪切并粘贴到地址栏中,我得到一个空白屏幕和一个: Refused to execute script from 'http://localhost:8080/reports/main.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. 尝试为 devServer 设置 contentBase 但这并没有解决问题。

此功能在 Webpack 3 和 RR3 中运行良好。不确定是否要吠叫 Webpack 或 React Router 树。

与此类似的问题,但针对 Webpack 3

javascript webpack webpack-dev-server react-router-v4

2
推荐指数
1
解决办法
1287
查看次数