React+vite+antd点击路由切换时出现闪屏

问题描述:点击左侧路由切换会出现闪屏的情况

页面的效果

解决方案

完整代码

javascript 复制代码
import { lazy, Suspense, ReactNode } from "react";
import { Navigate, useLocation } from "react-router-dom";
import LayOut from '../pages/LayOut/LayOut'
import NotFound from '../pages/NotFound/NotFound'
const LoginIndex = lazy(() => import('../pages/LoginIndex/LoginIndex'))
const DashBoard = lazy(() => import('../pages/DashBoard/DashBoard'))
const UserInfo = lazy(() => import('../pages/UserInfo/UserInfo'))
const SettingPage = lazy(() => import('../pages/SettingPage/SettingPage'))
const ItemList = lazy(() => import('../pages/ItemList/ItemList'))
const GoodsList = lazy(() => import('../pages/GoodsList/index'))
const BannerList = lazy(() => import('../pages/BannerList/index'))

const laztRoutes = (element: ReactNode): ReactNode => {
    return <Suspense fallback={<div>Loading...</div>}>
        {element} </Suspense>
}

const RequireAuth = ({ children }: { children: ReactNode }): ReactNode => {
    const location = useLocation();
    const token = localStorage.getItem('token'); // 假设token存储在localStorage中

    if (!token) {
        return <Navigate to="/login" state={{ from: location }} replace />;
    }

    return children;
}

export default [
    {
        path: '/',
        element: <Navigate to='/dashboard' />,
    },
    {
        path: '/',
        element: <RequireAuth><LayOut /></RequireAuth>,
        children: [

            {
                path: '/dashboard',
                element: laztRoutes(<DashBoard />)
            },
            {
                path: '/userinfo',
                element: laztRoutes(<UserInfo />)
            },
            {
                path: '/setting',
                element: laztRoutes(<SettingPage />)
            },
            {
                path: '/itemlist',
                element: laztRoutes(<ItemList />)
            },
            {
                path: '/goods',
                element: laztRoutes(<GoodsList />)
            },
            {
                path: '/banner',
                element: laztRoutes(<BannerList />)
            }
        ]
    },
    {
        path: '/login',
        element: <LoginIndex />
    },
    {
        path: '*',
        element: <NotFound />
    }
] 
相关推荐
前端Hardy几秒前
HTML&CSS:超炫丝滑的卡片水波纹效果
前端·javascript·css·3d·html
技术思考者4 分钟前
HTML速查
前端·css·html
缺少动力的火车5 分钟前
Java前端基础—HTML
java·前端·html
Domain-zhuo18 分钟前
Git和SVN有什么区别?
前端·javascript·vue.js·git·svn·webpack·node.js
雪球不会消失了23 分钟前
SpringMVC中的拦截器
java·开发语言·前端
李云龙I34 分钟前
解锁高效布局:Tab组件最佳实践指南
前端
m0_7482370538 分钟前
Monorepo pnpm 模式管理多个 web 项目
大数据·前端·elasticsearch
JinSoooo40 分钟前
pnpm monorepo 联调方案
前端·pnpm·monorepo
m0_748244961 小时前
【AI系统】LLVM 前端和优化层
前端·状态模式
明弟有理想1 小时前
Chrome RCE 漏洞复现
前端·chrome·漏洞·复现