reactjs React HashRouter不会在GitHub页面上呈现任何内容

aij0ehis  于 2023-02-18  发布在  React
关注(0)|答案(1)|浏览(138)

HashRouter不会在GitHub页面上呈现任何内容,控制台也不会显示任何错误。此外,只有当我在应用名称前加上#而不是输入:本地主机:3000/聊天应用程序我必须键入本地主机:3000/#/聊天应用程序。
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 />} />

vmdwslir

vmdwslir1#

从所有路由中删除"/chat-app",因为包含它将使绝对URLhttps://melosshabi.github.io/chat-app/#/chat-app和https://melosshabi.github.io/chat-app/#/chat-app/signin等。

如果需要的话,你可以在路由器上指定一个basename属性,我不认为在你的情况下需要它。
Route组件上也没有exact属性;在RRDv 6中,所有路由总是精确匹配。

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>
  );
}

相关问题