< Link>React with Typescript中的react-router-dom出错

gblwokeq  于 2023-02-17  发布在  TypeScript
关注(0)|答案(2)|浏览(174)

我遇到了一个关于"react-router-dom'"库的问题。这是我第一次使用React with Typescript;
一切都工作正常,直到我把'react-router-dom'当所有的页面下降;
基本上我想把三个项目的标题(主页,关于和登录),并改变页面,当我点击他们。错误来时,我把标签上的组件,如果我只是更新App.jsx和改变页面上写的URL,它的工作很好。

    • 应用程序jsx**
import 'bootstrap/dist/css/bootstrap.min.css';

// --- component ---
import MainHeader from './components/MainHeader';
import MainFooter from './components/MainFooter';

// --- pages ---
import { BrowserRouter, Routes, Route } from 'react-router-dom'
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';

// --- style ---
import './App.css';

function App() {
    return (
        <div className="App">
            <div className='sec-header'>
                <MainHeader />
            </div>
            <BrowserRouter>
                <Routes>
                    <Route path="/" element={<Home />}></Route>
                    <Route path="/about" element={<About />}></Route> 
                    <Route path="/contact" element={<Contact />}></Route> 
                </Routes>
            </BrowserRouter>
            <div className='sec-footer'>
                <MainFooter />
            </div>
        </div>
    );
}

export default App;
    • 主标题**
import './MainHeader.css'
import { Link } from 'react-router-dom'

function MainHeader() {

    return (
        <div className="p-3">
            <nav className="navbar navbar-expand">
                <ul className="rounded-4 d-flex m-auto p-0">
                    <li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
                        <Link to="/">Home</Link>
                    </li>
                    <li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
                        <Link to="/about">About</Link>
                    </li>
                    <li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
                        <Link to="/login">Login</Link>
                    </li>
                </ul>
            </nav>
        </div>
    )
}

export default MainHeader
    • 控制台上有2个错误**
1. Uncaught Error: useHref() may be used only in the context of a <Router> component.

2. react-dom.development.js:18687 The above error occurred in the <Link> component:

你知道会是什么吗?

avkwfej4

avkwfej41#

如下所示将Mainheader组件移动到BrowserRouter内部以解决此问题

import 'bootstrap/dist/css/bootstrap.min.css';

// --- component ---
import MainHeader from './components/MainHeader';
import MainFooter from './components/MainFooter';

// --- pages ---
import { BrowserRouter, Routes, Route } from 'react-router-dom'
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';

// --- style ---
import './App.css';

function App() {
    return (
        <div className="App">
           <BrowserRouter>
            <div className='sec-header'>
                <MainHeader />
            </div>

                <Routes>
                    <Route path="/" element={<Home />}></Route>
                    <Route path="/about" element={<About />}></Route> 
                    <Route path="/contact" element={<Contact />}></Route> 
                </Routes>
            </BrowserRouter>
            <div className='sec-footer'>
                <MainFooter />
            </div>
        </div>
    );
}
dauxcl2d

dauxcl2d2#

这可能是因为MainHeader组件位于app.jsx文件中的BrowserRouter组件之外。如果您希望标题在所有页面上都可见,请将其放在BrowserRouter组件之内,但位于Routes组件之外:

function App() {
return (
    <div className="App">
        <BrowserRouter>
            <div className='sec-header'>
                <MainHeader /> //THIS LINE HERE INSIDE BROWSERROUTER
            </div>
            <Routes>
                <Route path="/" element={<Home />}></Route>
                <Route path="/about" element={<About />}></Route> 
                <Route path="/contact" element={<Contact />}></Route> 
            </Routes>
        </BrowserRouter>
        <div className='sec-footer'>
            <MainFooter />
        </div>
    </div>
);

}

相关问题