简体   繁体   English

使用 Webpack 在 React 中加载图像

[英]Loading images in React with Webpack

I'm having trouble getting a simple image to show up in a simple app using Webpack and React.我无法使用 Webpack 和 React 在一个简单的应用程序中显示一个简单的图像。

I've read this thru and tried a few different ways, but keep getting various errors, or at best sometimes no errors, but also no image displaying.我已经通读了这篇文章并尝试了几种不同的方法,但不断收到各种错误,或者充其量有时没有错误,但也没有图像显示。

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

import React from 'react';
import styles from '../css/main.css';

import Menu from './Menu';

const logo = require('./images/PIVX_Planet_1a_239x83.png');

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {test: 'foo'};
  }
  render() {
    return (
      <div>
        <div id='container'></div>

        <div className={styles.logo}>
          <img src={logo}/>
        </div>
        <div>
          <Menu/>
        </div>
      </div>
    );
  }
}

Here is my webpack config:这是我的 webpack 配置:

...
module: {
    loaders: [{
      test: /\.jsx?$/,
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        "presets": ["react", "es2015", "stage-0", "react-hmre"]
      }
    }, {
      test: /\.(jpg|png|svg)$/,
      loader: 'url-loader',
      options: {
        limit: 25000,
      },
    }, {
      test: /\.json?$/,
      loader: 'json'
    }, {
      test: /\.css$/,
      loader: 'style!css?modules&localIdentName=[name]---[local]---[hash:base64:5]'
    }]
  }
...

With this, get error from webpack in console:有了这个,在控制台中从 webpack 得到错误:

ERROR in ./app/components/App.js Module not found: Error: Cannot resolve 'file' or 'directory' ./images/PIVX_Planet_1a_239x83.png in /Users/mac/_DEV/_LAB/PIVX/PIVX-Planet-2/app/components @ ./app/components/App.js 67:11-56未找到 ./app/components/App.js 模块中的错误:错误:无法解析 /Users/mac/_DEV/_LAB/PIVX/PIVX-Planet-2 中的“文件”或“目录”./images/PIVX_Planet_1a_239x83.png /app/components @ ./app/components/App.js 67:11-56

I've also tried using babel-plugin-transform-react-jsx-img-import ( https://www.npmjs.com/package/babel-plugin-transform-react-jsx-img-import )我也试过使用 babel-plugin-transform-react-jsx-img-import ( https://www.npmjs.com/package/babel-plugin-transform-react-jsx-img-import )

And then just use:然后只需使用:

<div className={styles.logo}>
  <img src="./images/PIVX_Planet_1a_239x83.png"/>
</div>

In that case, there are no errors, but the image show up broken.在这种情况下,没有错误,但图像显示已损坏。

I've tried changing the relative path to the image with all these combinations.我尝试使用所有这些组合更改图像的相对路径。

Directory structure:目录结构:

  • app应用程序
    • components组件
      • App.js应用程序.js
    • images图片
      • PIVX_Planet_1a_239x83.png PIVX_Planet_1a_239x83.png
    • index.html ...索引.html ...

Any insights?任何见解?

As per your directory structure the path that you need to use is 根据您的目录结构,您需要使用的路径是

const logo = require('../images/PIVX_Planet_1a_239x83.png');

since images is under app directory and not under components from where you are trying to get the image location 因为图像是在app目录,而不是根据components从正在试图获得的image位置

Also make sure all your loaders are properly installed 还要确保所有loaders都已正确安装

you can also try let as: 你也可以尝试让:

let logo = require('../images/PIVX_Planet_1a_239x83.png');

Always use let as much as possible to avoid the scope monster 始终尽量使用let以避免范围怪物

The problem I was having was using 我遇到的问题是使用

import logo from './images/PIVX_Planet_1a_239x83.png'

instead of 代替

const logo = require('./images/PIVX_Planet_1a_239x83.png');

in a typescript react app w/ a bespoke webpack config. 在打字稿反应应用程序与定制的webpack配置。

I have tried everything that you guys have suggested but nothing is working for me.我已经尝试了你们建议的所有内容,但没有任何效果对我有用。 when I put my code in and 'run dev' it come out with an error placeholder.当我将代码放入并“运行 dev”时,它会出现一个错误占位符。 I am using it in the app.js and would like my logo as a link to the home page or just show up at all, in this case I was attempting to just have it on the page.我在 app.js 中使用它,并希望我的徽标作为主页的链接或完全显示,在这种情况下,我试图将它放在页面上。

import '../styles/globals.css'
import Link from 'next/link'
import bpdlogofull from '../public/bpdlogofull.png'


    `function MyApp({ Component, pageProps }) {
      return (
        <div>
          <nav className="border-b p-6">
            <img src={bpdlogofull} alt='logo'className='flex justify-end'/>
            <div className="flex mt-4">
              <Link href="/" className="home-button">

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

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