使用反应路由器时没有出现任何内容

发布于 2025-01-09 04:18:08 字数 2421 浏览 0 评论 0原文

我的反应路由器有问题。

所以我正在构建一个网站的导航,当我做路线时,什么也没有出现。

你能帮我看看我做错了什么吗?

页面保持白色。什么也没有出现,甚至 root 也看不到主页。 ...................................................... ...................................... 主页:

import React from 'react';
import Navbar from '../allpages/navbar/Navbar';
import {
    BrowserRouter as Router, Routes, Route
} from "react-router-dom";
import Contact from "../contact/Contact";
import About from "../about/About";
const Home = () => {
    return (
        <>
            <Router>
                <Navbar />
                <Routes>
                    <Route  path="/about" element={About} />
                    <Route  path="/contact" element={Contact} />
                    <Route exact path="/" element={Home} />
                </Routes>
            </Router>

            <h3>Home</h3>
        </>
    )
}

export default Home

导航栏:


import React from 'react';

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

const Navbar = () => {
    return (
   
            <div>
                <nav>
                    <ul>
                        <li>
                            <Link to="/home">Home</Link>
                        </li>
                        <li>
                            <Link to="/about">About</Link>
                        </li>
                        <li>
                            <Link to="/contact">Contact</Link>
                        </li>
                    </ul>
                </nav>
            </div>
        
        
    )
}

export default Navbar

主应用程序:

import './App.css';
import Home from './components/home/Home'
function App() {
  return (
    <>
      <Home />
    </>
  );
}

export default App;

联系人:

import React from 'react';

const Contact = () => {
    return (
        <div>
            <h3>Contact</h3>
        </div>
    )
}

export default Contact

关于:

import React from 'react';
const About = () => {
    return (
        <div>
            <h1>About</h1>
        </div>
    )
}

export default About

I have a problem with react Router.

So i am building the nav of an website and when i'm doing the routes, nothing appears.

Would you help me please to see what i'm doing wrong?

The page stays white. Nothing appears, even the root doesnt see the homepage.
........................................................................................
Homepage:

import React from 'react';
import Navbar from '../allpages/navbar/Navbar';
import {
    BrowserRouter as Router, Routes, Route
} from "react-router-dom";
import Contact from "../contact/Contact";
import About from "../about/About";
const Home = () => {
    return (
        <>
            <Router>
                <Navbar />
                <Routes>
                    <Route  path="/about" element={About} />
                    <Route  path="/contact" element={Contact} />
                    <Route exact path="/" element={Home} />
                </Routes>
            </Router>

            <h3>Home</h3>
        </>
    )
}

export default Home

Navbar:


import React from 'react';

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

const Navbar = () => {
    return (
   
            <div>
                <nav>
                    <ul>
                        <li>
                            <Link to="/home">Home</Link>
                        </li>
                        <li>
                            <Link to="/about">About</Link>
                        </li>
                        <li>
                            <Link to="/contact">Contact</Link>
                        </li>
                    </ul>
                </nav>
            </div>
        
        
    )
}

export default Navbar

main app:

import './App.css';
import Home from './components/home/Home'
function App() {
  return (
    <>
      <Home />
    </>
  );
}

export default App;

contact:

import React from 'react';

const Contact = () => {
    return (
        <div>
            <h3>Contact</h3>
        </div>
    )
}

export default Contact

About:

import React from 'react';
const About = () => {
    return (
        <div>
            <h1>About</h1>
        </div>
    )
}

export default About

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

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

发布评论

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

评论(3

橪书 2025-01-16 04:18:08

因此,除了这个拼写错误:

 { BrowserRouter as Link } // use just Link

在 Navbar.js 内部,

我还会使用 React-Router 团队推荐的方法。它使用 组件来包装页面。您正在使用 Routes 代替,不知道这是否有效。
您可以查看以下示例: https://v5.reactrouter.com/web/example/basic< /a>
这与您的代码非常相似。

So besides this typo:

 { BrowserRouter as Link } // use just Link

which is inside Navbar.js

I would also use recommended approach by React-Router team. Which uses <Switch> component to wrap pages. You are using Routes instead, idk if this is even valid.
You can check this example: https://v5.reactrouter.com/web/example/basic
which is really similar to your code.

决绝 2025-01-16 04:18:08

您使用什么版本?你可能已经过时了

<React.Suspense fallback={<p>Loading...</p>}>
                <Routes>
                    <Route path="/*" element={<Outlet />}>
                  
                    <Route path="informacion-financiera" element={<Financiera />} />
                    <Route path="derechos-de-autor" element={<Author />} />
                    <Route 
                    </Route>
                </Routes>
            </React.Suspense>
    ```

What versión is you using? You might be out of dated

<React.Suspense fallback={<p>Loading...</p>}>
                <Routes>
                    <Route path="/*" element={<Outlet />}>
                  
                    <Route path="informacion-financiera" element={<Financiera />} />
                    <Route path="derechos-de-autor" element={<Author />} />
                    <Route 
                    </Route>
                </Routes>
            </React.Suspense>
    ```
橪书 2025-01-16 04:18:08

您使用什么版本?你可能已经过时了

<React.Suspense fallback={<p>Loading...</p>}>
                <Routes>
                    <Route path="/*" element={<Outlet />}>
                  
                    <Route path="informacion-financiera" element={<Financiera />} />
                    <Route path="derechos-de-autor" element={<Author />} />
                    <Route 
                    </Route>
                </Routes>
            </React.Suspense>
    ```


You have browserRouter as Link so yow links are not links

What versión is you using? You might be out of dated

<React.Suspense fallback={<p>Loading...</p>}>
                <Routes>
                    <Route path="/*" element={<Outlet />}>
                  
                    <Route path="informacion-financiera" element={<Financiera />} />
                    <Route path="derechos-de-autor" element={<Author />} />
                    <Route 
                    </Route>
                </Routes>
            </React.Suspense>
    ```


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