HashRouter
不会在 GitHub 页面上呈现任何内容,控制台也不会显示任何错误。此外,仅当我在应用程序名称前添加 # 时,它才会在本地呈现,因此我必须输入 localhost:3000/#/chat-app,而不是输入: localhost:3000/chat-app。
App.js
import SignIn from './Components/SignIn';
import SignUp from './Components/SignUp';
import Home from './Components/Home';
import UserProfile from './Components/UserProfile';
import { HashRouter, Routes, Route } from 'react-router-dom'
function App() {
return (
<HashRouter>
<Routes>
<Route path="/chat-app" exact element={<Home />} />
<Route path="/chat-app/signIn" exact element={<SignIn />} />
<Route path="/chat-app/signUp" exact element={<SignUp />} />
<Route path="/chat-app/userProfile" exact element={<UserProfile />} />
</Routes>
</HashRouter>
);
}
export default App;
package.json:
{
"homepage": "https://melosshabi.github.io/chat-app",
"name": "chat-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"firebase": "^9.16.0",
"nanoid": "^4.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.8.0",
"react-scripts": "5.0.1",
"universal-cookie": "^4.0.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"gh-pages": "^5.0.0"
}
}
我尝试了一些在网上找到的修复方法,但没有一个起作用。如果您需要此处的存储库链接,则为:https://github.com/melosshabi/chat-app
我尝试更改导入:import { HashRouter as Router, Routes, Route } from 'react-router-dom'
至:import { HashRouter, Routes, Route } from 'react-router-dom'
我尝试更改应用程序名称。
我也尝试更改:<Route path="/chat-app/home" element={<Home />} />
至:<Route path="/chat-app" exact element={<Home />} />
最佳答案
从所有路由中删除 "/chat-app"
因为包含此内容会使绝对 URL https://melosshabi.github.io/chat-app/#/chat-app和 https://melosshabi.github.io/chat-app/#/chat-app/signin等
如有必要,您可以在路由器上指定一个 basename
属性。我认为您的情况不需要这样做。
Route 组件上也没有 exact
属性;在 RRDv6 中,所有路由始终完全匹配。
function App() {
return (
<HashRouter basename="/chat-app"> // <-- if necessary
<Routes>
<Route path="/" element={<Home />} />
<Route path="/signIn" element={<SignIn />} />
<Route path="/signUp" element={<SignUp />} />
<Route path="/userProfile" element={<UserProfile />} />
</Routes>
</HashRouter>
);
}
关于javascript - React HashRouter 不会在 GitHub Pages 上呈现任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75464970/