简体   繁体   English

Webpack的静态图像无法加载

[英]Static images with Webpack not loading

I am working with React using Webpack and I am having trouble getting static images to load. 我正在使用Webpack与React一起工作,但无法加载静态图像。 The loader seems to be correctly converting the image to a URL but it doesn't seem to work as the img src when the page is rendered. 加载程序似乎已正确地将图像转换为URL,但在呈现页面时,它似乎不能用作img src。 The relative path of the images are correct. 图像的相对路径正确。 Here is my code below: 这是我的代码如下:

webpack.config.dev.js: 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' }
],
},

React code 反应代码

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;

I have tried a bunch of different ways using the url-loader and file-loader but I haven't had any luck yet. 我已经尝试使用url-loader和file-loader进行多种方式的尝试,但是我还没有碰到任何运气。 I would appreciate some help on this! 我希望对此有所帮助!

Thank you. 谢谢。

Looks like from webpack configuration that you're running twice url-loader over the same file types. 从webpack配置看来,您正在同一文件类型上运行两次url-loader This might break your images output. 这可能会破坏您的图像输出。

What you see is a base64 encoding of the image. 您看到的是图像的base64编码。 I wasn't familiar with the 'url-loader', but a quick look at their page Says that it loads images as base64. 我对'url-loader'并不熟悉,但是快速浏览了一下他们的页面说它将图像加载为base64。 You can find some useful info about it here: https://css-tricks.com/data-uris/ You might want to consider a different loader if you prefer the image path and not a base64. 您可以在此处找到一些有用的信息: https ://css-tricks.com/data-uris/如果您更喜欢映像路径而不是base64,则可能需要考虑使用其他加载器。 Hope it helps. 希望能帮助到你。

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

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