javascript 我正在学习RECT,在使用一个组件的时刻,它被放置在第一个组件下面

b1payxdu  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(88)

正如标题中所说,我正在学习,所以我想这样做

import Navbar from "../components/navbar";
import Footer from "../components/footer";
import Contactanos from "../components/contactanos";

export default function ContactanosPage() {
  return (
    <>
      <Navbar />
      <Contactanos />
      <Footer />
    </>
  );
}

当我转到本例中的路径是localhost:300/contacts时,组件一个叠加在另一个的顶部,除了页脚,它位于所有内容的下面。
在这里我发送组件的代码

import '../css/footer.css'
export default function Footer() {
    return (
        <div className="pie-pagina">
            <div className="grupo-2">
                <small>&copy; 2023 <b>Los Mixturiados</b> - Todos los Derechos Reservados.</small>
            </div>
        </div>
    )
}
import { Link } from "react-router-dom";
import '../css/navbar.css'

export default function Navbar() {

    return (
        <>
            <div className="contenedor-header">
                <header>
                    <div className="logo">
                        <a href="/">Los Mixturiados</a>

                    </div>
                    <nav className="nav">
                        <ul>
                            <li><Link to={'/'}>INICIO</Link></li>
                            <li><Link to={'/menus'}>MENUS</Link></li>
                            <li><Link to={'/contacto'}>CONTACTAME</Link></li>
                        </ul>
                    </nav>
                    <div className="nav-responsive">
                        <i className="fa-solid fa-bars"></i>
                    </div>
                </header>

            </div></>
    )
}

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import reportWebVitals from './reportWebVitals';
import { createBrowserRouter, RouterProvider } from 'react-router-dom'
import Home from './routes/Home';
import Menus from './components/Menus';
import ContactanosPage from './routes/ContactanosComp';

const router = createBrowserRouter([
  {
    path: '/',
    element: <Home />,
    errorElement: <h1>Ocurrio un Error</h1>
  },
  {
    path: '/menus',
    element: <Menus />,
  },
  {
    path: '/contacto',
    element: <ContactanosPage/>,
  }
]);

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <RouterProvider router={router} />
  </React.StrictMode>
);

reportWebVitals();

我试图尽一切努力创建另一个div,用insibile div来做<></>我已经尝试了一切,但我不能让它很好地适应我可以改进什么或我可以改变什么?

4xy9mtcn

4xy9mtcn1#

如果你只是想让NavbarFooter在页面上***无条件地***呈现,而所有路由的内容,例如HomeMenusContactos,在它们之间***有条件地***呈现,例如:在路线上,那么建议是为主UI创建布局路线。布局路由将呈现Navbar、用于嵌套路由内容的OutletFooter组件。
示例:

import { Outlet } from 'react-router-dom';
import Navbar from "../components/navbar";
import Footer from "../components/footer";

const AppLayout = () => (
  <>
    <Navbar />
    <Outlet />
    <Footer />
  </>
);

export default AppLayout;

导入AppLayout并集成到路由配置中。

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import reportWebVitals from './reportWebVitals';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import AppLayout from '../components/AppLayout';
import Home from './routes/Home';
import Menus from './components/Menus';
import Contactanos from '../components/contactanos';

const router = createBrowserRouter([
  {
    element: <AppLayout />,
    children: [
      {
        path: '/',
        element: <Home />,
        errorElement: <h1>Ocurrio un Error</h1>
      },
      {
        path: '/menus',
        element: <Menus />,
      },
      {
        path: '/contacto',
        element: <Contactanos />,
      }
    ]
  },
]);

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <RouterProvider router={router} />
  </React.StrictMode>
);

相关问题