当使用时,我的图像文件夹应该在哪个目录下?
在src或public中
当我准备运行yarn build时,其中一个或另一个会出现问题吗?
我听说只有放置在public目录中的文件才会在构建后加载。真伪
我想index.html上的react团队的这个评论让我很困惑。
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder
during the build.
Only files inside the `public` folder can be referenced from
the HTML.
Unlike "/favicon.ico" or "favicon.ico",
"%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->编辑:在阅读了下面的注释后,这是如何在src文件夹中构造comments文件夹的。你能告诉我这是否是一个好/最好的做法吗?
**Images Component**
import Foo from "./images/foo.jpg";
import Bar from "./images/bar.jpg";
import './styles/Images.css';
export const Logo = () => (
<img src={Foo} alt="bla bla" />
)
export const Profile = () => (
<img src={Bar} alt="bla bla" />
)--我想使用这些图像的组件.
import { Logo, Profile } from './Images';
const Home = () => (
<div>
<Logo/>
<Profile/>
</div>
)发布于 2018-02-04 00:53:11
您应该在src下为图像创建一个文件夹。然后,只需使用import './myImage.png' as imageThatIsMine将它们导入.js文件。
在.css中,您将使用
.myClass {
background-image: url('./myImage.png')
}这样做将确保webpack正确地将文件移动到build文件夹并附加正确的路径。
有关更多信息,请参见文档。
如果您将任何内容放置在src文件夹之外并尝试导入,您将得到一个错误,告诉您使用src。
您可以将项目放置在;公用文件夹中,但不建议这样做。请参阅文档
如果确实使用公用文件夹,则在使用公用文件夹时需要使用%PUBLIC_URL%前缀。
https://stackoverflow.com/questions/48603554
复制相似问题