未加载 Webpack 的静态图像

Cla*_*ars 3 javascript urlloader reactjs webpack

我正在使用 Webpack 使用 React,但无法加载静态图像。加载程序似乎正确地将图像转换为 URL,但在呈现页面时它似乎不能作为 img src。图片的相对路径正确。这是我的代码如下:

webpack.config.dev.js:

module: {
loaders: [
  {
    test: /\.css$/,
    exclude: /node_modules/,
    loader: 'style-loader!css-loader?localIdentName=[name]__[local]__[hash:base64:5]&modules&importLoaders=1&sourceMap!postcss-loader',
  }, {
    test: /\.css$/,
    include: /node_modules/,
    loaders: ['style-loader', 'css-loader'],
  }, {
    test: /\.jsx*$/,
    exclude: [/node_modules/, /.+\.config.js/],
    loader: 'babel',
  }, {
    test: /\.(jpe?g|gif|png|svg)$/i,
    loader: 'url-loader',
    options: {
      limit: 25000,
    },
  }, {
    test: /\.json$/,
    loader: 'json-loader',
  }, { 
    test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' }
],
},
Run Code Online (Sandbox Code Playgroud)

反应代码

import React, { Component} from 'react';

import styles from './Album.css';

const logo = require('./img/MoP.png');

export class Album extends Component{
  constructor(props){
    super(props);

    console.log(logo);
}

render(){
    return (
        <div className = {styles.album}>
            <div className="row">
                <div className="col-8">
                    <div className="albumInfo">
                        <h6> {this.props.title} </h6>
                        <h6> {this.props.artist} </h6>
                        <h6> {this.props.date} </h6>
                        <h6> Rating: {this.props.rating} </h6>
                        <h6> {this.props.comment} </h6>
                    </div>
                </div>
                <div className="col-4 align-self-center">
                    <img src={logo}></img>
                </div>
            </div>
        </div>
    )
}
}

export default Album;
Run Code Online (Sandbox Code Playgroud)

我使用 url-loader 和 file-loader 尝试了很多不同的方法,但我还没有运气。我将不胜感激!

谢谢你。

And*_*aro 6

从 webpack 配置来看,您正在url-loader对相同的文件类型运行两次。这可能会破坏您的图像输出。

  • 我的崩溃了,因为我在同一个文件上运行 file-loader 和 url-loader (2认同)