简体   繁体   English

传递给 Reusable-Component 时的图像源 (src) 不会在 UI 中显示图像

[英]Image Source (src) when passed to Reusable-Component does not show image in UI

I have added an image in the public folder of React-Project.我在 React-Project 的public文件夹中添加了一个图像。 Now, when I want to show an image - it gets showed up in a component, but if I try to pass its url and show the image in some reusable component - I find that image is not shown in UI.现在,当我想显示图像时 - 它显示在组件中,但如果我尝试传递它的 url 并在某些reusable component中显示图像 - 我发现该图像未显示在 UI 中。

Can someone help me out?有人可以帮我吗?

Home.js主页.js

import React, { Component } from 'react';
import Cards from './cards';

class Home1 extends Component {

  render(){
    return(
      <div>
        <img src="img/aishwarya.jpg" />
        <Cards title="Aishwarya" sourcelink="img/aishwarya.png" details="Miss World 1994" />
      </div>
    )
  }

}

export default Home1;

Cards.js Cards.js

import React from 'react';
import './cards.css';

const Cards = (props) => {

  return (
    <div className="cardHolder">
      <div className="imageHolder">
        <img src={props.sourcelink} />
      </div>
      <div className="contentHolder">
        <h3>{props.title}</h3>
        <p>{props.details}</p>
      </div>
    </div>
  );

}

export default Cards;

Output: Output:

在此处输入图像描述

When using webpack , for images under src , a path like img/aishwarya.jpg is only available during build time .使用webpack时,对于src下的图像,类似img/aishwarya.jpg的路径仅在构建期间可用。

You need to import it, see Adding Images, Fonts, and Files :您需要导入它,请参阅添加图像、Fonts 和文件

import image from "./img/aishwarya.jpg";
console.log(image) // /aishwarya.84287d09.jpg

<img src={image} />
<img src={require("img/aishwarya.jpg")} />

See working example:请参阅工作示例:

编辑interesting-smoke-zv0pv

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

相关问题 src不存在时更改图像源(两次) - Change image source (twice) when src does not exist 在将图像源作为属性传递的组件中反应延迟加载图像 - React lazy load image in a component that is passed image source as a property 当源作为变量传入时,无法渲染图像 - Cannot render an image when source passed in as a variable Material UI 有图像组件吗? - Does Material UI have an Image component? 谷歌云存储图像 url 工作正常,但是当在 img html 文件的 src 中使用时,它会转到替代项并且不显示图像 - google cloud storage image url works fine but when used in the src of an img html file it goes to the alternative and does not show the image 如果图像标记中没有源(src =“”),如何避免使用十字标记符号 - How to avoid cross mark symbol when there is no source in image tag (src=“ ” ) 更改图像源而不更新范围时的角度ng-src - Angular ng-src when changing source of an image not updating scope 具有无 ID 图像的可重用 React 复选框组件 - Reusable React checkbox component with image without ID 反应无状态组件中的onMouseEnter div时如何更改图像src - How to change image src when onMouseEnter div in react stateless component 作为道具传递的反应图像src不起作用 - React image src passed as props not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM