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 78 79 80
| import { lazy,Suspense } from 'react' import App from 'App' import { BrowserRouter, Routes,Route, Navigate } from "react-router-dom";
interface routeType{ path?:string element?:React.ReactNode children?:Array<routeType> }
interface IRoute { path:string; component:React.FC; children?:Array<IRoute> }
const router_arr:IRoute[] = [ { path:"/", component:App, children:[ {path:"/home",component:lazy(()=>import("pages/Home"))}, {path:"/sort",component:lazy(()=>import("pages/Sort"))}, {path:"/shopcar",component:lazy(()=>import("pages/ShopCar"))}, {path:"/usercenter",component:lazy(()=>import("pages/UserCenter"))}, {path:"/",component:lazy(()=>import("pages/Home"))}, ] }, { path:"/login", component:lazy(()=>import("pages/Login")) }, { path:"/search", component:lazy(()=>import("pages/Search")) }, { path:"/register", component:lazy(()=>import("pages/Register")) }, { path:"*", component:lazy(()=>import("pages/404")) } ]
const MyRouter = ()=>( <BrowserRouter> <Suspense fallback={<h1></h1>}> <Routes>{ router_arr.map((item,index)=>{ if (item.path==='*') { return <Route key={index} path={item.path} element={<item.component/>}></Route> } return (item.children ? <Route key={index} path={item.path} element={<item.component/>}> { item.children.map((e,i)=>{ if (e.path === '/') { return <Route key={i} path={e.path} element={<Navigate to="/home"/>}/> } return <Route key={i} path={e.path} element={<e.component/>}/> }) } </Route>: <Route key={index} path={item.path} element={<item.component/>}></Route> )}) }</Routes> </Suspense> </BrowserRouter> )
export default MyRouter
|