javascript react-router-dom v6不显示页面

8gsdolmq  于 2023-05-05  发布在  Java
关注(0)|答案(3)|浏览(227)

我有一个应用程序,成功地路由的网址,但由于某种原因,网页不显示在标题下。我已经看了其他的问题和可用的资源,但我很难弄清楚我做错了什么。我正在使用react-router-dom 6.3.0
以下是我的文件供参考:Index.js:

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import { BrowserRouter as Router } from "react-router-dom";

import App from "./App";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
    <Router>
        <App />
    </Router>
);

App.js:

import "./App.css";

import { Routes, Route } from "react-router-dom";

import Header from "./components/Header";

import Home from "./pages/Home";
import Freshman from "./pages/Freshman";
import Sophomore from "./pages/Sophomore";
import Junior from "./pages/Junior";
import Senior from "./pages/Senior";

function App() {
    return (
        <div className='app'>
            <Header />
            <Routes>
                <Route path="/" element={<Home />} />
                <Route path="freshman" element={<Freshman />} />
                <Route path="sophomore" element={<Sophomore />} />
                <Route path="junior" element={<Junior />} />
                <Route path="senior" element={<Senior />} />
            </Routes>
        </div>
    );
}

export default App;

Header.js:

import React from "react";

import { Link } from "react-router-dom";

import "./Header.css";

function Header() {
    return (
        <div className="header-container">
            <Link to="/" className="header-title">
                College
            </Link>
            <nav>
                <Link to="freshman" className="nav-item">
                    Freshman
                </Link>
                <Link  to="sophomore" className="nav-item">
                    Sophomore
                </Link>
                <Link to="junior" className="nav-item">
                    Junior
                </Link>
                <Link to="senior" className="nav-item">
                    Senior
                </Link>
            </nav>
        </div>
    );
}

export default Header;

Home.js:

import React from "react";

function Home() {
    return (
        <div>
            <h1>home</h1>
        </div>
    );
}

export default Home;

所有的页面都只是一个标签,其标题为黑色字体。然而,就像我说的,即使URL被正确路由,也没有一个页面被显示。任何帮助将不胜感激!

6pp0gazn

6pp0gazn1#

你的代码似乎是正确的!可以尝试在每个路由前添加**”/“**。
如果您在预览中看到白色,请回复...

34gzjxbg

34gzjxbg2#

您需要一个<Outlet />元素来呈现路由。根据您的代码,看起来这应该在Home.js的return语句中:

import React from "react";

function Home() {
    return (
        <div>
            <h1>home</h1>
            <Outlet />
        </div>
    );
}

export default Home;

这将根据所选择的路由将新生、softmore、初级、高级组件呈现为Home的子组件。
您可以在这里阅读<Outlet />的文档:https://reactrouter.com/docs/en/v6/components/outlet

x3naxklr

x3naxklr3#

对于react-router-dom V6,您需要在index.js文件中呈现根,如下面的代码所示。并在App.js中编写代码,如下所示。

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import {BrowserRouter} from "react-router-dom";

const root = ReactDOM.createRoot(document.getElementById("root"))
root.render(
<BrowserRouter>
<App />
</BrowserRouter>
);

App.js

import About from"./routes/About"
import Home from "./routes/Home.js"
import { Route, Routes } from "react-router-dom";

function App() {
return (

<>
 <Routes>
  <Route path="/" element={<Home />} />
  <Route path="/about" element={<About />} />
 </Routes>

 </>
);

}
导出默认App;

相关问题