我是在 reactJS 中将图像资产存储在 public 还是 src 中? [英] Do I store Image assets in public or src in reactJS?

查看:43
本文介绍了我是在 reactJS 中将图像资产存储在 public 还是 src 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用 react.我有一个 div 我想要一个背景图片.但我无法显示.

I am using react for my application. I have a div that I would like to have a background image. But I can't get it to show.

当我将它作为 myapp/src/bgimage.png 包含在 src 文件夹中时,它运行良好,但我听说我应该将它包含在名为images 在根级别,所以它是 myapp/images/bgimage.png,但是这对我不起作用并给了我:

When I include it in the src folder as myapp/src/bgimage.png it works perfectly but I've heard that I should include it in a folder named images at the root level so it's myapp/images/bgimage.png, however this does not work for me and gives me:

您试图导入位于项目 src/目录之外的 ../images/bgimage.png.'

You attempted to import ../images/bgimage.png which falls outside of the project src/ directory.'

谁能告诉我在 reactJS 中包含图像资源的正确方法吗?

Can anyone tell me the proper way to include image assets in reactJS?

推荐答案

public:你的应用在编译时没有使用的任何东西

public: anything that is not used by your app when it compiles

src:编译应用程序时使用的任何东西

src: anything that is used when the app is compiled

因此,例如,如果您在组件内使用图像,则它应该位于 src 文件夹中,但如果您在应用程序外部(即收藏夹图标)中有图像,则它应该是公开的.

So for example if you use an image inside a component, it should be in the src folder but if you have an image outside the app (i.e. favicon) it should be in public.

这篇关于我是在 reactJS 中将图像资产存储在 public 还是 src 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆