1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| {} import { Navigate } from 'react-router-dom' import Home from '../pages/Home' import Login from '../pages/Login' import Register from '../pages/Register' import Mine from '../pages/Mine'
import Detail from '../pages/Home/Detail'
export const routes = [ { path:'/Home/*', element: <Home />, children:[ { path:'Detail', element: <Detail /> }, ] }, { path:'/Login', element: <Login /> }, { path:'/Register', element: <Register /> }, { path:'/Mine', element: <Mine /> }, { path:'/', element: <Navigate to="/Home" /> }, ]
import { useRoutes } from 'react-router-dom' import {routes} from './routes' const App = () => { const element = useRoutes(routes) return( <NavLink to="login">登录</NavLink> <NavLink to="register">注册</NavLink> <NavLink to="home">主页</NavLink> <NavLink to="mine">我的</NavLink> {} <Routes> <Route path="/Login" element={<Login />}></Route> <Route path="/Register" element={<Register />}></Route> <Route path="/Home/*" element={<Home />}></Route> {} <Route path="/Mine" element={<Mine />}></Route> <Route path="/" element={<Navigate to="/home" />}></Route> </Routes> {} {element} ) }
import React from 'react' import { NavLink,Outlet } from 'react-router-dom' const Home = () => { return ( <> <div>主页</div> <br /> <NavLink to="detail">详情</NavLink> <Outlet /> // 和 {element} 相似 </> ) } export default Home
|