javascript 使用React-Router-Dom的React JS中嵌套路由不起作用

wrrgggsh  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(222)

我正在尝试创建一个Dashboard,但是我被嵌套的路由卡住了。就像我想在屏幕上有导航栏,侧边栏和页脚,但是只有主要的组件应该改变。
我在两个文件中使用了路由:App.jsHome.jsx
App.js:

import React from "react";
import "./App.css";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Login from "./pages/login/Login";
import Registeration from "./pages/registeration/Registeration";
import Home from "./pages/Home/Home";

function App() {
  return (
    <div className="App">
      <Router>
        <Routes>
          <Route
            path="/"
            element={
              <React.Fragment>
                <div className="login_page">
                  <Login />
                </div>
              </React.Fragment>
            }
          />
          <Route
            path="registeration"
            element={
              <React.Fragment>
                <div className="login_page">
                  <Registeration />
                </div>
              </React.Fragment>
            }
          />

          <Route path="home" element={<Home />} />
        </Routes>
      </Router>
    </div>
  );
}

export default App;

Home.jsx:

import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Assets from "../../components/assets/Assets";
import Balance from "../../components/balance/Balance";
import Dashboard from "../../components/dashboard/Dashboard";
import { Footer } from "../../components/Footer/Footer";
import Navbar from "../../components/navbar/Navbar";
import OpenAccount from "../../components/openAccount/OpenAccount";
import Sidebar from "../../components/sidebar/Sidebar";
import "./Home.scss";

const Home = () => {
  return (
    <div className="home">
      <Sidebar />
      <div className="homeContainer">
        <Navbar />
        <div>
          <Routes>
            <Route path="/balance" element={<Balance />} />
            <Route path="/" element={<Dashboard />} />
            <Route path="/assets" element={<Assets />} />
            <Route path="/open-account" element={<OpenAccount />} />
          </Routes>
        </div>
        <Footer />
      </div>
    </div>
  );
};

export default Home;
2g32fytz

2g32fytz1#

看起来您已经在App.js和Home.jsx文件中正确设置了路由,但是您在Home.jsx中使用的是嵌套路由,这意味着您需要使用react-router-dom中的Outlet组件来呈现子路由。
下面是更正后的Home.jsx文件:

import React from "react";
import { BrowserRouter as Router, Routes, Route, Outlet } from "react-router-dom";
import Assets from "../../components/assets/Assets";
import Balance from "../../components/balance/Balance";
import Dashboard from "../../components/dashboard/Dashboard";
import { Footer } from "../../components/Footer/Footer";
import Navbar from "../../components/navbar/Navbar";
import OpenAccount from "../../components/openAccount/OpenAccount";
import Sidebar from "../../components/sidebar/Sidebar";
import "./Home.scss";

const Home = () => {
  return (
    <div className="home">
      <Sidebar />
      <div className="homeContainer">
        <Navbar />
        <div>
          <Outlet />
        </div>
        <Footer />
      </div>
    </div>
  );
};

export default Home;

请注意,我已经用Outlet组件替换了子路由,这将呈现Home组件内部的嵌套路由。
另外,在App.js文件中,Registeration页面的Route组件的路径prop中有一个拼写错误,您将“registration”写成了“registeration”。

bsxbgnwa

bsxbgnwa2#

在当前实现中,Home组件正在呈现***后代***路由,而不是嵌套路由。为了使后代路由可用于匹配并呈现其路由内容,父路由必须在其路径中附加尾随通配符"*"匹配器。
示例:

<Route path="home/*" element={<Home />} />

另请注意,派生路由是相对于其父路由构建的,因此Home路由将解析为:

<Route
  path="/balance"           // <-- "/home/balance"
  element={<Balance />}
/>
<Route
  path="/"                  // <-- "/home"
  element={<Dashboard />}
/>
<Route
  path="/assets"            // <-- "/home/assets"
  element={<Assets />}
/>
<Route
  path="/open-account"      // <-- "/home/open-account"
  element={<OpenAccount />}
/>

通过将Home组件转换为布局路由组件,您也可以使用***嵌套***路由实现相同的目的。布局路由为嵌套路由呈现Outlet组件,以便将element内容呈现到该组件中,而不是直接呈现后代路由。
示例:
一个二个一个一个

相关问题