Chr*_*ine 19 github-pages single-page-application reactjs
我使用 React 和 React Router 构建了我的网站,它托管在 Github Pages 上。当我在不是我的主页的页面上刷新网站或按住 ctrl+单击在新选项卡中打开页面时,会导致 404 错误。我知道这是因为 Github 页面无法访问前端路由,一种解决方案是添加一个重定向回 index.html 的 404.html 文件。
我尝试按照两者的说明进行操作
但两者都不适合我。我想我错过了一些东西,但我不知道出了什么问题,因为我对 React Router 不太熟悉。有人可以帮忙吗?(注意:我知道解决方案是使用 HashRouter,但我不希望我的 URL 看起来很难看)
我的代码可以在 GitHub 上查看: https: //github.com/christinexlin/portfolio
小智 14
我花了相当长的时间来解决这个问题。问题是GitHub Pages不支持单页面应用程序,因此刷新页面时会出现404错误。
查看https://github.com/rafgraph/spa-github-pages并按照说明进行操作,这非常简单。我遵循了“基本说明”,但请查看“详细说明”中的步骤 5,它帮助我完全修复了它(将存储库名称添加到 index.html 中资产的绝对路径并将segmentCount 设置为 1)。
看看我的代码https://github.com/milosrancic/reactjs-website。我没用过HashRouter,我用过Switch。我还添加了 404.html 文件。
我希望这有帮助
您可以尝试解决此问题的一种方法是在 App.js 中替换Switch
为。HashRouter
这会更改您的 URL,但应该可以解决 github 页面的 404 问题。如果您需要更深入地解释为什么会发生这种情况,请阅读此回复。
所以你的更新App.js
应该是这样的:
import React, { Component } from "react";
import { HashRouter, Route } from "react-router-dom";
import Emoji from "react-emoji-render";
import "./App.css";
//Pages
import Projects from "./Projects.js";
import About from "./About.js";
import Critterpedia from "./Critterpedia.js";
import Bluenotes from "./Bluenotes.js";
import Formally from "./Formally.js";
//Components
import visualize from "./Images/visualize-actualize.png";
import ScrollToTop from "./ScrollToTop.js";
class App extends Component {
render() {
return (
<div>
<ScrollToTop />
<HashRouter>
<Route exact path="/" component={Projects} />
<Route path="/about" component={About} />
<Route path="/critterpedia" component={Critterpedia} />
<Route path="/bluenotes" component={Bluenotes} />
<Route path="/formally" component={Formally} />
</HashRouter>
<div className="footer">
<div className="emoji">
<Emoji text="" />
</div>
<div className="intro-icon">
<div className="img-div">
<img src={visualize} alt="visualize and actualize" />
</div>
</div>
<div className="credit">
<div className="footer-links">
<a href="https://github.com/christinexlin">GitHub</a>
<a href="https://www.linkedin.com/in/christine-lin-01/">
LinkedIn
</a>
<a href="https://twitter.com/christinexlin">Twitter</a>
</div>
<p>
Made with <Emoji text="" />
by Christine Lin
</p>
</div>
</div>
</div>
);
}
}
export default App;
Run Code Online (Sandbox Code Playgroud)
让我知道这是否适合您
归档时间: |
|
查看次数: |
14124 次 |
最近记录: |