React Router V6总是渲染“//&quot”

发布于 2025-02-12 03:36:11 字数 552 浏览 0 评论 0原文

我正在尝试在React-Create-App中实现路由器,但它总是呈现“/”并显示Home或Signin页面。我该如何解决?

function AppRouter({ isLoggedIn, user }) {
  return(
    <Router>
      <Routes>
        <Route path="/profile" element={<Profile />} />
        <Route path="/signUp" element={<SignUp />} />
        {isLoggedIn
          ? <Route exact path={"/"} element={<Home user={user}/>} />
          : <Route exact path={"/"} element={<SignIn />} />
        }
      </Routes>
    </Router>
  )
}

I'm trying to implement router in react-create-app but it always render "/" and showing Home or SignIn page. How can I solve this?

function AppRouter({ isLoggedIn, user }) {
  return(
    <Router>
      <Routes>
        <Route path="/profile" element={<Profile />} />
        <Route path="/signUp" element={<SignUp />} />
        {isLoggedIn
          ? <Route exact path={"/"} element={<Home user={user}/>} />
          : <Route exact path={"/"} element={<SignIn />} />
        }
      </Routes>
    </Router>
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2025-02-19 03:36:11

看来您对hashrouter与UI的工作方式有轻微的误解。

import { HashRouter as Router, Route, Routes } from "react-router-dom";
import Profile from "./Profile";
import SignUp from "./SignUp";
import Home from "./Home";

export default function App() {
  return (
    <Router>
      <Routes>
        <Route path="/profile" element={<Profile />} />
        <Route path="/signUp" element={<SignUp />} />
        <Route path="/" element={<Home />} />
      </Routes>
    </Router>
  );
}

hashrouter处理带有URL哈希值的路由,即在URL中的所有内容 “#”。如果您试图渲染应用程序并访问“&lt; domain&gt;/“,而不是”&lt; domain&gt;/#/“/#/”路由无法正常工作。

例如,在您的运行代码和盒子演示中,基本URL为“ https://5p7hff.csb.app/”。在此基础网址上,哈希路由器无法真正工作,您应该真正访问“ https://5p7hff.csb.app.app/#/”而不是加载哈希路由器,并加载了应用程序。内部路由可以起作用。

来自“ https://5p7hff.csb.app/#/”您应该导航到任何路由,即 ”

/profile“ 和https://5p7hff.csb.app/#/signup ?fontsize = 14&amp; hidenAvigation = 1&amp; inditialpath =%2f%23%2F&amp; module =%2FSRC%2fapp.js&amp; themp; them = dark“ rel =“ rel =“ nofollow noreferrer” /static/img/play-codesandbox.svg“ alt =”编辑Yound-Dawn-ejyruw“>

如果您切换到其他路由器,例如browserrouter the ,则” /#/“不再使用,路由器和路由从”/“”渲染。应用程序从哪里运行。路由将为“ https:// 5p7hff。 csb.app/“” https://5p7hff.csb.app/profile“” https://5p7hff.csb.app/signup”代码>。

It seems you have a slight misunderstanding of how the HashRouter works with the UI.

import { HashRouter as Router, Route, Routes } from "react-router-dom";
import Profile from "./Profile";
import SignUp from "./SignUp";
import Home from "./Home";

export default function App() {
  return (
    <Router>
      <Routes>
        <Route path="/profile" element={<Profile />} />
        <Route path="/signUp" element={<SignUp />} />
        <Route path="/" element={<Home />} />
      </Routes>
    </Router>
  );
}

The HashRouter handles routing with a URL hash value, i.e. everything after the "#" in the URL. If you are trying to render your app and access "<domain>/" instead of "<domain>/#/" the routing won't work.

For example in your running codesandbox demo, the base URL is "https://5p7hff.csb.app/". At this base URL the hash router isn't really working, and you should really be accessing "https://5p7hff.csb.app/#/" instead so the hash router is loaded and the app's internal routing can work.

From "https://5p7hff.csb.app/#/" you should be to then navigate to any of your routes, i.e. "https://5p7hff.csb.app/#/profile" and https://5p7hff.csb.app/#/signUp".

Edit young-dawn-ejyruw

If you switch to a different router, like the BrowserRouter then the "/#/" is no longer used, the router and routes render from "/" where the app is running from. The routes would be "https://5p7hff.csb.app/", "https://5p7hff.csb.app/profile", and "https://5p7hff.csb.app/signUp".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文