您的位置:首页 > 技术中心 > 前端框架 >

react build 路径不对怎么办

时间:2023-01-18 16:38

react build路径不对的解决办法:1、找到“node_modules -> react-scripts -> config -> paths.js”;2、修改内容为“envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : './');”;3、重新编译打包即可。

本教程操作环境:Windows10系统、react18.0.0版、Dell G3电脑。

react build 路径不对怎么办?

React项目build之后资源文件路径不正确或打开空白页的问题及简易解决方法

找到node_modules -> react-scripts -> config -> paths.js

修改

function getServedPath(appPackageJson) {
  const publicUrl = getPublicUrl(appPackageJson);
  const servedUrl =
    envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');//改成'./'
  return ensureSlash(servedUrl, true);
}

function getServedPath(appPackageJson) {
  const publicUrl = getPublicUrl(appPackageJson);
  const servedUrl =
    envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : './');
  return ensureSlash(servedUrl, true);
}

再重新编译打包即可

或者更简便的方法是在你的package.json:加入下面这句

“homepage”: “.”,

这将确保所有资产路径都相对于index.html

这样应用从中移动http://mywebsite.com到http://mywebsite.com/relativepath甚至http://mywebsite.com/relative/path无需重建。

如果您没有使用HTML5 pushState历史记录API或根本不使用客户端路由,则无需指定应用程序的URL。

如果还是资源路径不对,可能recat框架用了BrowserRouter路由会导致浏览器访问不到相应的路由配置,这个路由有点小问题,可以更换HashRouter即可解决空白或资源路径错误问题.

推荐学习:《react视频教程》

以上就是react build 路径不对怎么办的详细内容,更多请关注gxlsystem.com其它相关文章!

热门排行

今日推荐

热门手游