gpt4 book ai didi

reactjs - 部署到 Github 页面时未加载图像

转载 作者:行者123 更新时间:2023-12-04 13:59:59 26 4
gpt4 key购买 nike

我正在使用 create-react-app开发我的 React 应用程序并将其部署到 Github 页面。我的应用程序在开发中正常工作。但是当我部署时,它成功部署但背景图像加载失败。

index.css 中加载背景图像文件:

body::after {
background-image: url("/background-img.jpg");
}

主页上 package.json
{
"homepage": "http://bigfanjs.github.io/my-repo/",
}

应用程序目录上的背景图像:
| build
| background-img.jpg
| src
| public
| background-img.jpg

在控制台上:

GET https://bigfanjs.github.io/background-img.jpg 404 ()



最后,当我将开发人员工具上的 URL 编辑为 url(/my-repo/background-img.jpg)它加载。

最佳答案

我遇到了这个问题,我通过在您使用图像的任何 .js 文件中显式导入图像文件来解决它。
所以如果你的相对路径是 './images/chess.png' ,您可以使用 import chess from './images/chess.png' 导入它
有关更多信息,请参阅此链接:https://create-react-app.dev/docs/adding-images-fonts-and-files

关于reactjs - 部署到 Github 页面时未加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51002481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com