图像未显示在路由/ React build vs Dev中

时间:2019-03-25 20:52:48

标签: reactjs webpack react-routing

我正在尝试让我的图片显示在我的React代码的构建版本中。

```import reportIcon from "../../../../../src/img/reportmanager.svg"; 
<img src={reportIcon } className="img-icons-list" />```

此代码在构建版本中有效。我的reportmanager图标出现,但是当我导航到www.mywebsite.com/reports/user时,该图标消失了     import reportIcon from "/src/img/reportmanager.svg";

也不起作用。这是我的webpack.config.js文件

```const HtmlWebPackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
var path = require('path');
module.exports = {
entry: './src/index.js',
output: {
    path: path.resolve(__dirname + '/public'),
    filename: 'bundle.js'
},
module: {
    rules: [
        {
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            options: {
                presets: ['@babel/preset-env',
                    '@babel/react', {
                        'plugins': ['@babel/plugin-proposal-class-properties']
                    }]
            },
        },
        {
            test: /\.css$/,
            use: ExtractTextPlugin.extract({
                fallback: "style-loader",
                use: "css-loader"
            })
        },
        {
            test: /\.(eot|ttf|woff|woff2)$/,
            loader: 'file-loader?name=./font/[name]_[hash:7].[ext]'
        },
        {
            test: /\.(jpg|png|svg)$/,
            loader: 'file-loader?name=./img/[name]_[hash:7].[ext]'
        }

    ]
},
devServer: {
    historyApiFallback: {
        index: "/",
    }
},
plugins: [
    new ExtractTextPlugin("css/style.css"),
    new HtmlWebPackPlugin({
        template: "./src/index.html",
        filename: "./index.html"
    }),
    function () {
        this.plugin("done", function (stats) {
            if (stats.compilation.errors && stats.compilation.errors.length && process.argv.indexOf('--watch') == -1) {
                console.log(stats.compilation.errors);
                process.exit(1); // or throw new Error('webpack build failed.');
            }
            // ...
        });
    }
]
};

1 个答案:

答案 0 :(得分:1)

我需要放

<base href="/">

在我的React项目的index.html中。