首页 > 解决方案 > 如何修复 GitHub Pages 中的反应框架错误

问题描述

我正在学习 React 并在 Github 中设置一个 repo,它位于https://github.com/chenjianAgain/ContactManager_Source.git

我使用 npm run build 在我的笔记本电脑中生成构建目录。并且服务构建工作正常。

但是当我将整个构建目录复制到下面的新仓库时,Github Pages 无法访问它。https://github.com/chenjianAgain/ContactManager_Destination.git

有人可以帮助我吗?非常感谢。

https://github.com/chenjianAgain/ContactManager_Source.git

源代码在我的笔记本电脑上运行良好。

serve build也适用于我的笔记本电脑。

https://github.com/chenjianAgain/ContactManager_Destination.git

但是当我将所有文件从 /build 复制到上面的 repo 时,Github Pages 不起作用。

标签: reactjsgithub

解决方案


转到您的 package.json 并添加此属性

    "homepage": "http://username.github.io/myapp" 

我的猜测是你错过了这一步。

此外,如果您使用的是 react-router,如果您在应用程序上使用子目录,则必须添加基本名称。

    <BrowserRouter basename="/myapp">
      <App />
    </BrowserRouter>,

推荐阅读