Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 当url为多层时,无法访问bundle.js_Reactjs_Webpack_Webpack 4 - Fatal编程技术网

Reactjs 当url为多层时,无法访问bundle.js

Reactjs 当url为多层时,无法访问bundle.js,reactjs,webpack,webpack-4,Reactjs,Webpack,Webpack 4,bundle.js适用于localhost:3000/abc(因为它在localhost:3000/assets文件夹中查找),但是如果我转到localhost:3000/abc/xyz我会得到一个404(因为它在localhost:3000/abc/assets文件夹中查找) 我的webpack.config.js var webpack = require("webpack"); var path = require('path') module.exports = { entry:

bundle.js
适用于
localhost:3000/abc
(因为它在
localhost:3000/assets
文件夹中查找),但是如果我转到
localhost:3000/abc/xyz
我会得到一个
404
(因为它在
localhost:3000/abc/assets
文件夹中查找)

我的
webpack.config.js

var webpack = require("webpack");
var path = require('path')
module.exports = {
    entry: "./src/index.js",
    output: {
        path: path.join(__dirname, '/dist'),
        filename: "bundle.js",
        publicPath: "/assets/",
    },
    devServer: {
        inline: true,
        contentBase: "./dist",
        port: 3000,
        historyApiFallback: true,
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                loader: "babel-loader",
                query: {
                    presets: ["@babel/preset-env", "@babel/preset-react"] 
                }
            },
            {
                type: 'javascript/auto',
                test: /\.html/,
                exclude: /(node_modules)/,
            },
            {
                test: /\.css$/,
                use:['style-loader','css-loader']
            },
            {
                test: /\.scss$/,
                use:['style-loader','css-loader', 'sass-loader']
            }
        ]
    }
}
我不能为
publicPath
使用绝对路径。
当我有多层时,如何访问
bundle.js

<script type="text/javascript" src="./assets/bundle.js"></script>

我所要做的就是把它改成

<script type="text/javascript" src="/assets/bundle.js"></script>