简体   繁体   English

反应 - 无法在 Heroku 上渲染图像

[英]React - Unable to render images on Heroku

I deployed a full stack web application on heroku.我在 heroku 上部署了一个完整的堆栈 web 应用程序。

The react images rendered using css files are showing fine on the deployed webpage.使用css 文件渲染的反应图像在部署的网页上显示良好。 But the ones which are loading using import img from './img.jpg' are not showing on the webpage.但是使用import img from './img.jpg'加载的那些没有显示在网页上。

NOTE : They are showing fine on local machine (dev).注意:它们在本地机器 (dev) 上显示良好。

What could be the reason and what would be the solution to this?可能是什么原因以及解决方案是什么?

React Component:反应组件:

// images
import American from './American1.jpg';
import Italian from './Italian1.jpg';
import Indian from './Indian1.jpg';
import Chinese from './Chinese1.jpg';


// array for images
const foods = [American, Indian, Italian, Chinese];

// how they're being rendered
<div className="row justify-content-between">
    {foods.map((food, i) => {
        return (
            <div className="col-sm-6 col-lg-3 my-3" key={foodsNames[i]}>
                <div className="row justify-content-center">
                    <div className="col-11">
                        <Card className="MainCard">
                            <div className="card-image">
                                
                             // images used below
                                <Card.Img className="image" variant="top" src={food} />
                             // image used above 

                            </div>

                            <Card.Body>
                                <Card.Title className="card-title">{foodsNames[i]}</Card.Title>
                                <Card.Text>
                                </Card.Text>
                            </Card.Body>
                        </Card>
                    </div>
                </div>
            </div>

        )
    })}
</div>




Move your images to public folder, and access it like below in your component.将您的图像移动到公共文件夹,并在您的组件中访问它,如下所示。 I have my image (brand_icon.png) in direcory public/images我的图片 (brand_icon.png) 在目录 public/images

<img alt='' src={'../images/brand_icon.png'} />

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

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