简体   繁体   English

传递道具时图像未加载<img src=“this.prop.” />在反应中

[英]Image not loading when passing a prop to <img src=“this.prop.” /> in React

I have an issue regarding a link not loading my image when I pass a prop to the attribute src of img.当我将道具传递给 img 的属性 src 时,我遇到了关于链接未加载我的图像的问题。 I have tried wrapping require() around my links and it didn't work.我曾尝试将 require() 包裹在我的链接中,但没有奏效。

I have a huge library of products which would be unfeasible to manually import images using the image loader.我有一个庞大的产品库,使用图像加载器手动导入图像是不可行的。

Therefore, is there a way to load an image using props inside the src attribute?因此,有没有办法在 src 属性中使用 props 加载图像?

I am using NODE_PATH=src/我正在使用 NODE_PATH=src/

Thanks for the help!谢谢您的帮助!

 export const productLib = [{ ID: "001", name:"product name", src: "lib/productLib/img/product_name", color: "", ... }, {...}]
 import React from 'react'; export class Products extends React.Component { render() { return( <div className="products"> <img src={this.props.product.src} width="50" /> <h6>{this.props.product.name}<span className={`block ${this.props.product.color}`}></span></h6> </div> ) } }

As recommended by Xuscrus, I linked my images using the public path.按照 Xuscrus 的建议,我使用公共路径链接了我的图像。 I had to remove my absolute path in order for this solution to work.我必须删除我的绝对路径才能使此解决方案起作用。

I moved all of my images inside a folder named "img" inside the public folder of my create-react-app.我将所有图像移动到我的 create-react-app 公共文件夹中名为“img”的文件夹中。

With the example used in my initial statement, here is the easiest solution I found:对于我最初声明中使用的示例,这是我找到的最简单的解决方案:

 import React from 'react'; export class Products extends React.Component { render() { return( <div className="products"> <img src={`/img/${this.props.product.src}.png`} width="50" /> <h6>{this.props.product.name}<span className={`block ${this.props.product.color}`}></span></h6> </div> ) } }

My main reference for my solution was this post (URL method in the correct answer) :我的解决方案的主要参考是这篇文章(正确答案中的 URL 方法):

How to do Dynamic images in ReactJS? 如何在 ReactJS 中做动态图像?

I think issue is related with the way you are sending the url to the component.我认为问题与您将 url 发送到组件的方式有关。

"lib/productLib/img/product_name" is not an url, not relative url also. “lib/productLib/img/product_name”不是网址,也不是相对网址。

You can use import to get the image.您可以使用导入来获取图像。

 import React from 'react'; import logo from './logo.png'; // Tell webpack this JS file uses this image console.log(logo); // /logo.84287d09.png function Header() { // Import result is the URL of your image return <img src={logo} alt="Logo" />; } export default Header;

If you are using react-create app如果您使用的是 react-create 应用程序

they resolve the problem for you: https://create-react-app.dev/docs/adding-images-fonts-and-files/他们为您解决问题: https : //create-react-app.dev/docs/adding-images-fonts-and-files/

I came across a similar issue in fact I was searching for the solution at the moment but figured it out myself I have Images store in a separate folder and I'm importing them in the data file and then Passing it to the component where I needed it, but the image was not displaying.我遇到了一个类似的问题,事实上我当时正在寻找解决方案,但我自己发现我将图像存储在一个单独的文件夹中,我将它们导入到数据文件中,然后将其传递给我需要的组件它,但图像没有显示。

All I did was我所做的只是

//img is the variable I'm getting the image path at
console.log(img)

That returned那返回

Module {default: "/static/media/svg-1.af890272.svg", __esModule: true, 
Symbol(Symbol.toStringTag): "Module"}
default: "/static/media/svg-1.af890272.svg"
Symbol(Symbol.toStringTag): "Module"
__esModule: true
[[Prototype]]: Object

After Getting this in the Module Object I saw the default value so just do在模块对象中得到这个之后,我看到了默认值,所以就这样做

img = img.default

And now you can pass it in src现在你可以在 src 中传递它

import React from 'react';从“反应”导入反应;

<img src={img} width="50" />
              

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

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