react build路径不对如何解决

2023-05-22,

这篇文章主要介绍“react build路径不对如何解决”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“react build路径不对如何解决”文章能帮助大家解决问题。

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

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 build路径不对如何解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注本站行业资讯频道,小编每天都会为大家更新不同的知识点。

《react build路径不对如何解决.doc》

下载本文的Word格式文档,以方便收藏与打印。