缓加载优化

This commit is contained in:
unknown 2023-07-24 10:15:23 +08:00
parent 79982b0ca8
commit 86972e55a3
7 changed files with 69 additions and 24 deletions

View File

@ -1,7 +1,9 @@
import React, { useEffect } from "react"; import { useEffect } from "react";
import styles from "./index.module.less"; import styles from "./index.module.less";
import { Outlet } from "react-router-dom"; import { Outlet } from "react-router-dom";
import { Header, LeftMenu } from "../../compenents"; import { Header, LeftMenu } from "../../../compenents";
import { Suspense } from "react";
import LoadingPage from "../../loading";
const HomePage = () => { const HomePage = () => {
useEffect(() => {}, []); useEffect(() => {}, []);
@ -17,8 +19,10 @@ const HomePage = () => {
<Header></Header> <Header></Header>
</div> </div>
<div className={styles["right-main"]}> <div className={styles["right-main"]}>
{/* 二级路由出口 */} <Suspense fallback={<LoadingPage height="100vh" />}>
<Outlet /> {/* 二级路由出口 */}
<Outlet />{" "}
</Suspense>
</div> </div>
</div> </div>
</div> </div>

View File

@ -0,0 +1,8 @@
.layout-wrap {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
}

View File

@ -0,0 +1,16 @@
import { Suspense } from "react";
import styles from "./index.module.less";
import { Outlet } from "react-router-dom";
import LoadingPage from "../../loading";
const WithoutHeaderWithoutFooter = () => {
return (
<div className={styles["layout-wrap"]}>
<Suspense fallback={<LoadingPage height="100vh" />}>
<Outlet />
</Suspense>
</div>
);
};
export default WithoutHeaderWithoutFooter;

View File

@ -1,6 +1,6 @@
.loading-parent-box { .loading-box {
width: 100vd; width: 100vw;
height: 100vh;
text-align: center;
line-height: 100vh; line-height: 100vh;
text-align: center;
} }

View File

@ -1,11 +1,20 @@
import { Spin } from "antd"; import { Spin } from "antd";
import styles from "./index.module.less"; import styles from "./index.module.less";
const LoadingPage = () => { interface PropsInterface {
height?: string;
}
const LoadingPage = (props: PropsInterface) => {
return ( return (
<div className={styles["loading-parent-box"]}> <>
<Spin size="large" /> <div
</div> className={styles["loading-box"]}
style={{ height: props.height || "100vh" }}
>
<Spin size="large" />
</div>
</>
); );
}; };

View File

@ -7,7 +7,9 @@ import KeepAlive from "../compenents/keep-alive";
// 页面加载 // 页面加载
import InitPage from "../pages/init"; import InitPage from "../pages/init";
import LoginPage from "../pages/login"; import LoginPage from "../pages/login";
import HomePage from "../pages/home"; import WithHeaderWithoutFooter from "../pages/layouts/with-header-without-footer";
import WithoutHeaderWithoutFooter from "../pages/layouts/without-header-without-footer";
//首页 //首页
const DashboardPage = lazy(() => import("../pages/dashboard")); const DashboardPage = lazy(() => import("../pages/dashboard"));
//修改密码页面 //修改密码页面
@ -77,7 +79,7 @@ const routes: RouteObject[] = [
children: [ children: [
{ {
path: "/", path: "/",
element: <PrivateRoute Component={<HomePage />} />, element: <PrivateRoute Component={<WithHeaderWithoutFooter />} />,
children: [ children: [
{ {
path: "/", path: "/",
@ -150,16 +152,22 @@ const routes: RouteObject[] = [
], ],
}, },
{ {
path: "/login", path: "/",
element: <LoginPage />, element: <WithoutHeaderWithoutFooter />,
}, children: [
{ {
path: "/test", path: "/login",
element: <TestPage />, element: <LoginPage />,
}, },
{ {
path: "*", path: "/test",
element: <ErrorPage />, element: <TestPage />,
},
{
path: "*",
element: <ErrorPage />,
},
],
}, },
], ],
}, },