问题
如题。
原因
来看一下build后终端中显示的内容。可以看到,该项目是在假定位于/的情况下构建的,即应该放在根目录下的。所以直接打开会因为路径问题加载不到部分资源。
$ react-scripts build
Creating an optimized production build...
Compiled successfully.
File sizes after gzip:
63.4 KB build\static\css\2.a2026422.chunk.css
61.01 KB build\static\js\2.5dc86013.chunk.js
772 B build\static\js\runtime-main.7a26b4d8.js
686 B build\static\js\main.32b40db7.chunk.js
547 B build\static\css\main.50695a3d.chunk.css
The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.
The build folder is ready to be deployed.
You may serve it with a static server:
yarn global add serve
serve -s build
Find out more about deployment here:
bit.ly/CRA-deploy
Done in 66.09s.
解决办法
在提示信息中已经给出来了,可以通过配置package.json文件中的homepage字段来解决。在最外层中加入以下键值对。
"homepage": "./",