问题

如题。

原因

来看一下build后终端中显示的内容。可以看到,该项目是在假定位于/的情况下构建的,即应该放在根目录下的。所以直接打开会因为路径问题加载不到部分资源。

  1. $ react-scripts build
  2. Creating an optimized production build...
  3. Compiled successfully.
  4. File sizes after gzip:
  5. 63.4 KB build\static\css\2.a2026422.chunk.css
  6. 61.01 KB build\static\js\2.5dc86013.chunk.js
  7. 772 B build\static\js\runtime-main.7a26b4d8.js
  8. 686 B build\static\js\main.32b40db7.chunk.js
  9. 547 B build\static\css\main.50695a3d.chunk.css
  10. The project was built assuming it is hosted at /.
  11. You can control this with the homepage field in your package.json.
  12. The build folder is ready to be deployed.
  13. You may serve it with a static server:
  14. yarn global add serve
  15. serve -s build
  16. Find out more about deployment here:
  17. bit.ly/CRA-deploy
  18. Done in 66.09s.

解决办法

在提示信息中已经给出来了,可以通过配置package.json文件中的homepage字段来解决。在最外层中加入以下键值对。

  1. "homepage": "./",