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 />
    }
] 
相关推荐
耶啵奶膘1 小时前
uniapp-是否删除
linux·前端·uni-app
王哈哈^_^3 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
cs_dn_Jie3 小时前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic4 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿4 小时前
webWorker基本用法
前端·javascript·vue.js
cy玩具5 小时前
点击评论详情,跳到评论页面,携带对象参数写法:
前端
customer085 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
清灵xmf5 小时前
TypeScript 类型进阶指南
javascript·typescript·泛型·t·infer
小白学大数据5 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
qq_390161775 小时前
防抖函数--应用场景及示例
前端·javascript