[英]Github pages HashRouter not displaying components
我在 github 頁面上的反應應用程序沒有使用 HashRouter 渲染組件,“/”上的空白頁面和“/about”上的 404 錯誤。 這是我的 app.js
function App() {
return (
<div className="Container-fluid" id="div2">
<HashRouter basename="/Landing-Page">
<Header />
<Routes>
<Route path="/thankyou" element={<ThankYou />} />
<Route
exact
path="/"
element={
<div className="Container">
<SignUp validate={validate} />
<EbookInfo />
</div>
}
/>
<Route path="/about" element={<About />} />
</Routes>
</HashRouter>
</div>
);
}
在主頁上它按預期顯示.../Landing-Page/
但在其他頁面上顯示.../about
而不是.../Landing-Page/about
。 我有 gh-pages 設置。 my github page https://alexhmar.github.io/Landing-Page/ my repo https://github.com/alexhmar/Landing-Page/tree/master
我也用<Router basename={process.env.PUBLIC_URL}
試過這個,但這是同樣的問題。
當使用 Github Pages 和 React Router Dom 時,您需要使用Hash Router 。
通過import { HashRouter as Router } from "react-router-dom";
它應該可以工作。
Github Pages isn't configured for single page applications, if you try to access /bar, it will try to find a bar.html
file and thus give you a 404. By using the hash router, when you navigate to links it will go到/#bar
所以 Github 頁面不會重定向你,React Router Dom 會看到它導航到 /bar 就像你期望的那樣,只要你使用Link 組件或鈎子。
我部署在 Heroku 而不是 Github 頁面上,現在它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.