reactjs < Link>使用“React路由器dom 6.7.0”

kmpatx3s  于 2023-01-30  发布在  React
关注(0)|答案(2)|浏览(155)

我正在尝试创建我的第一个网站,但是在重定向到另一个页面时遇到了问题。我将Routes添加到App.jsx,并添加了指向Navbar的链接;在Navbar中单击"服务"会转到"/services",但页面上什么也不显示,只有Navbar。我也尝试了NavLink,但没有任何变化。请帮助我找出问题所在。

import React from 'react';
import "./App.css";
import { BrowserRouter, Routes,Route } from "react-router-dom";
import Home from './pages/home/Home';
import Samples from './pages/samples/Samples';
import Services from './pages/services/Services';
import Navbar from './components/navbar/Navbar';
import Reviews from './components/reviews/Reviews';
import Contacts from './components/contacts/Contacts';

const App = () => {
  return (
    <div>
      <BrowserRouter>
        <Navbar />
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/services" element={<Services />} />
          <Route path="/samples" element={<Samples />} />
          {/* <Route path="#reviews" element={<Reviews />} /> */}
        </Routes>
      </BrowserRouter>
    </div>
  )
}

export default App
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { RiMenuFill, RiCloseLine } from 'react-icons/ri';
import './navbar.css';
import MainLogo from "../../assets/MainLogo.png";

const Navbar = () => {
  const [toggleMenu, setToggleMenu] = useState(false); 

  return (
    <div>
      <div className='navbar'>
        <Link style={{textDecoration:"none"}} to="/">
          <img src={MainLogo} alt="main_logo" className="navbar_logo"/>
        </Link>
        <ul className='navbar_links'>
          <li>
            <Link to="/services">Services</Link>
          </li>
          <li>
            <Link to="/samples">Samples</Link>
          </li>
          <li>
            <Link to="/">Reviews</Link>
          </li>
          <li>
            <Link to="#gallery">Gallery</Link>
          </li>
          <li>
            <Link to="#about">About Us</Link>
          </li>
          <li>
            <Link to="#contacts">Contacts</Link>
          </li>
        </ul>
      </div>
b1zrtrql

b1zrtrql1#

我在index.js中添加了{BrowserRouter},它工作正常

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

const root = ReactDOM.createRoot(document.getElementById('root'))
root.render(
    <React.StrictMode>
        <BrowserRouter>
        <App />
        </BrowserRouter>
    </React.StrictMode>
)
b1payxdu

b1payxdu2#

路由没有问题,这里的问题是导航栏使用固定定位,"/services"路由内容呈现在导航栏下。
App组件一些顶部边距,以便将内容向下推到导航栏下方。
示例:

const App = () => {
  return (
    <div className='app'> // <-- app container
      <BrowserRouter>
        <Navbar />
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/services" element={<Services />} />
          <Route path="/samples" element={<Samples />} />
          {/* <Route path="#reviews" element={<Reviews />} /> */}

        </Routes>
        {/* <Reviews /> */}
      </BrowserRouter>
    </div>
  )
}

app.css

.app {
  margin-top: 93px; // <-- header/navbar height
}

相关问题