feat: 懒加载拆分

This commit is contained in:
mozzie 2023-03-06 10:54:33 +08:00
parent 657bc8f69c
commit b9e421cb2e
3 changed files with 41 additions and 11 deletions

View File

@ -1,7 +1,9 @@
import { Spin } from "@arco-design/web-react";
import { Suspense } from "react";
import { Route, Routes, useNavigate } from "react-router-dom"; import { Route, Routes, useNavigate } from "react-router-dom";
import "./assets/base.less"; import "./assets/base.less";
import Nav from "./components/Nav"; import Nav from "./components/Nav";
import { routerList } from "./router"; import { commonRouters, lazyRouters } from "./router";
import { Guard } from "./router/Guard"; import { Guard } from "./router/Guard";
function App() { function App() {
@ -10,13 +12,24 @@ function App() {
<Nav /> <Nav />
<main> <main>
<Routes> <Routes>
{routerList.map((router) => ( {commonRouters.map((router) => (
<Route <Route
key={router.path} key={router.path}
path={router.path} path={router.path}
element={<Guard>{router.element}</Guard>} element={<Guard>{router.element}</Guard>}
/> />
))} ))}
{lazyRouters.map((router) => (
<Route
key={router.path}
path={router.path}
element={
<Suspense fallback={<Spin />}>
<Guard>{<router.element />}</Guard>
</Suspense>
}
/>
))}
<Route path="*" element={<span>404</span>} /> <Route path="*" element={<span>404</span>} />
</Routes> </Routes>
</main> </main>

View File

@ -1,6 +1,6 @@
import "./index.less"; import "./index.less";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { routerList } from "../../router"; import { commonRouters, menuRouters } from "../../router";
import { import {
Input, Input,
Modal, Modal,
@ -58,7 +58,7 @@ function Nav() {
<span>Backset</span> <span>Backset</span>
</div> </div>
<div className="middle"> <div className="middle">
{routerList.map( {menuRouters.map(
(route) => (route) =>
!route.invisible && ( !route.invisible && (
<span key={route.path} onClick={() => navigate(route.path)}> <span key={route.path} onClick={() => navigate(route.path)}>

View File

@ -1,28 +1,45 @@
import { lazy } from "react";
import Course from "../view/Course"; import Course from "../view/Course";
import CourseDetail from "../view/CourseDetail";
import Subscribe from "../view/Subscribe";
import Topic from "../view/Topic";
export const routerList = [ export interface IRouteMenuItem {
path: string;
name: string;
}
interface IRoute extends IRouteMenuItem {
element: JSX.Element | any;
invisible?: boolean;
}
export const commonRouters: IRoute[] = [
{ {
path: "/", path: "/",
element: <Course />, element: <Course />,
name: "学习", name: "学习",
}, },
];
export const lazyRouters: IRoute[] = [
{ {
path: "/topic", path: "/topic",
element: <Topic />, element: lazy(() => import("../view/Topic")),
name: "讨论", name: "讨论",
}, },
{ {
path: "/subscribe", path: "/subscribe",
element: <Subscribe />, element: lazy(() => import("../view/Subscribe")),
name: "订阅", name: "订阅",
}, },
{ {
path: "/course/detail/:id", path: "/course/detail/:id",
element: <CourseDetail />, element: lazy(() => import("../view/CourseDetail")),
name: "课程详情", name: "课程详情",
invisible: true, invisible: true,
}, },
]; ];
export const menuRouters = [...commonRouters, ...lazyRouters].map((route) => {
const { name, path } = route;
const invisible = "invisible" in route ? route.invisible : false;
return { name, path, invisible };
});