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

big*_*nJS 8 github-pages reactjs create-react-app

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

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

body::after {
  background-image: url("/background-img.jpg");
}
Run Code Online (Sandbox Code Playgroud)

主页在package.json

{
  "homepage": "http://bigfanjs.github.io/my-repo/",
}
Run Code Online (Sandbox Code Playgroud)

应用程序目录中的背景图片:

| build
    | background-img.jpg
| src
| public
    | background-img.jpg
Run Code Online (Sandbox Code Playgroud)

在控制台上:

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

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

小智 -1

更新

你标记了create-react-app,你一步步按照README操作了吗?它应该工作得很好。


404 not find 的主要原因是因为基本路径不匹配/

publicPath: /my-repo/output您的webpack.config.js.

参考:https: //github.com/webpack/docs/wiki/configuration#outputpublicpath