![在这里插入图片描](https://img-blog.csdnimg.cn/935229a9b5bc4954a8a8e01b17e7d9e8.png)
import React from 'react';
import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';
// 定义路由对应的组件
const Home = () => <h1>Home</h1>;
const About = () => <h1>About</h1>;
const Contact = () => <h1>Contact</h1>;
// 使用Router组件包裹根组件
const App = () => (
<Router>
<nav>
<ul>
{/* 添加导航链接 */}
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/contact">Contact</Link>
</li>
</ul>
</nav>
{/* 使用Switch组件确保只匹配一个路由 */}
<Switch>
{/* 定义路由规则 */}
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
</Router>
);
export default App;