[英]Why does my react-router Link not take me to the other page (React JS)?
I have this code inside my App.js and i declared those routes:我的 App.js 中有这段代码,我声明了这些路由:
function App() {
return (
<BrowserRouter>
<div className='App'>
<Switch>
<Route exact path="/"><Index /></Route>
<Route exact path="/contact"><Contact /></Route>
</Switch>
</div>
</BrowserRouter>
);
}
export default App;
And then in my Navbar.js i declared this link to /contact:然后在我的 Navbar.js 中,我将此链接声明为 /contact:
<Link to={ "/contact" } className="nav-item nav-link">Contact</Link>
And it doesn't work, the url changes into "http://localhost:3000/contact" but it remains in the index page, and the weird thing is that when i type exactly the same url in the url box it takes me to the contact page而且它不起作用,url 更改为“http://localhost:3000/contact”,但它仍保留在索引页面中,奇怪的是当我在 Z572D4E4E421E5E6B9BC11D815E8A027112Z 框中键入完全相同的 url 时,它需要到联系页面
Your code wrong it shold be like that: to="/contact"你的代码错了,应该是这样的:to="/contact"
Syntax error.语法错误。
You wrote: Link to={ "/contact" } but it will be like: <Link to="/contact">Contact</Link>
您写道: Link to={ "/contact" } 但它会像:
<Link to="/contact">Contact</Link>
I think It will be solved.我想会解决的。
Well I would alter the code slightly:好吧,我会稍微改变一下代码:
import { BrowserRouter, Route, Routes } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<div className='App'>
<Routes>
<Route index element={<Index />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</div>
</BrowserRouter>
);
}
export default App;
The link code you have I've altered slightly too:您拥有的链接代码我也略有更改:
<Link to="/contact" className="nav-item nav-link">Contact</Link>
That's how we have ours and it is working fine, should be for you too.这就是我们拥有我们的方式,并且工作正常,也应该适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.