闲来无事,用React的Router写了一个Demo
- import React from "react";
- import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
- // 首页组件
- function Home() {
- return <h2 style={{ margin: 0 }}>首页</h2>;
- }
- // 关于我们组件
- function About() {
- return <h2 style={{ margin: 0 }}>关于我们</h2>;
- }
- // 主应用组件
- function App() {
- return (
- <div
- style={{
- minHeight: "100vh",
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- background: "#f5f7fa",
- }}
- >
- <Router>
- <nav
- style={{
- marginBottom: "2rem",
- background: "#fff",
- padding: "12px 32px",
- borderRadius: "12px",
- boxShadow: "0 2px 12px rgba(0,0,0,0.08)",
- display: "flex",
- gap: "1.5rem",
- fontSize: "1.1rem",
- fontWeight: 500,
- }}
- >
- <Link to="/" style={{ textDecoration: "none", color: "#222" }}>
- 首页
- </Link>
- <span style={{ color: "#ccc" }}>|</span>
- <Link to="/about" style={{ textDecoration: "none", color: "#222" }}>
- 关于
- </Link>
- </nav>
- <div
- style={{
- background: "#fff",
- padding: "32px 48px",
- borderRadius: "16px",
- boxShadow: "0 4px 24px rgba(0,0,0,0.10)",
- minWidth: "260px",
- textAlign: "center",
- }}
- >
- <Routes>
- <Route path="/" element={<Home />} />
- <Route path="/about" element={<About />} />
- </Routes>
- </div>
- </Router>
- </div>
- );
- }
- export default App;
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|