我第一次尝试React-Router,并尝试编写一些代码,但链接似乎不起作用。这些是我的链接。起初,当我将链接放入应用程序组件中时,链接起作用了。然后我尝试将导航放入它自己的组件中,然后链接不起作用。感谢您的帮助!
import React from "react";
import About from "./About";
import Home from "./Home";
import Navigation from "./Navigation";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
export default function App() {
return (
<Router>
<div>
<Navigation />
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/" exact>
<Home />
</Route>
</Switch>
</div>
</Router>
);
}
import React from "react";
import { BrowserRouter as Link } from "react-router-dom";
export default function Navigation() {
return (
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
</nav>
);
}
慕容3067478
相关分类