简体   繁体   English

使用 react 和 webpack 添加图像时出错

[英]Error adding image with react and webpack

I am building a small react app usind webpack 3.x and when I try to show my local images these ones are not load.我正在使用 webpack 3.x 构建一个小型反应应用程序,当我尝试显示我的本地图像时,这些图像没有加载。 I get the error: Module not found: Error: Can´t resolve '/images/williamshakespeare.jpg'.我收到错误:找不到模块:错误:无法解析“/images/williamshakespeare.jpg”。

This is my React component:这是我的 React 组件:

import * as React from 'react';

export const Quiz = (props) => {
    return (
        <div className='row'>
            <div className='col-md-4'>
                <img src={require('/images/williamshakespeare.jpg')}/>
            </div>
        </div>
    );
}

This is my webpack.config.js.这是我的 webpack.config.js。 I have installed url-loader and file-loder.我已经安装了 url-loader 和 file-loder。

var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var basePath = __dirname;

module.exports = {
    context: path.join(basePath, "src"),
    resolve: {
        extensions: ['.js', '.jsx']
    },
    // The entry point for the bundle.
    entry: {
        // App entry point.
        app: './main.jsx',
        appStyles: [
            './css/style.css',
        ],
        vendor: [
            "react",
            "react-dom"
        ],
        vendorStyle: [
            '../node_modules/bootstrap/dist/css/bootstrap.css',
            '../node_modules/font-awesome/css/font-awesome.css',
        ],
    },
    output: {
        path: path.join(basePath, "dist"),
        filename: '[name].js',
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/, // The Condition must match
                exclude: /node_modules/, // The Condition must NOT match
                loader: 'babel-loader',
            },
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract({
                  fallback: 'style-loader',
                  use: {
                    loader: 'css-loader',
                  },
                }),
            },
            // Loading glyphicons => https://github.com/gowravshekar/bootstrap-webpack
            // Using here url-loader and file-loader
            {
                test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
                loader: 'url-loader?limit=10000&mimetype=application/font-woff'
            },
            {
                test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
                loader: 'url-loader?limit=10000&mimetype=application/octet-stream'
            },
            {
                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
                loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
            },
            {
                test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
                loader: 'file-loader'
            },
            {
                test: /\.(png|jpg)$/,
                exclude: /node_modules/,
                loader: 'url-loader?limit=5000',
            },
        ],
    },
    devtool: 'inline-source-map',
    devServer: {
        contentBase: './dist', // Content base
        inline: true, // Enable watch and live reload
        host: 'localhost',
        port: 8081,
        stats: 'errors-only' // To show only errors in your bundle
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html', // Name of file in ./dist/
            template: 'index.html', // Name of template in ./src
            hash: true // Append a unique webpack compilation hash to all included scripts and CSS files. This is useful for cache busting.
        }),
        new ExtractTextPlugin({
            filename: '[chunkhash].[name].css',
            disable: false,
            allChunks: true,
        }),
    ]
};

And this is my folders structure:这是我的文件夹结构:

在此处输入图片说明

Well, my fault, the correct path is好吧,我的错,正确的路径是

<img src={require('./images/williamshakespeare.jpg')}/>

instead of代替

<img src={require('/images/williamshakespeare.jpg')}/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM