欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > React+vite+antd点击路由切换时出现闪屏

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

2024/11/30 20:42:09 来源:https://blog.csdn.net/GAGGAAAAA/article/details/141057308  浏览:    关键词:React+vite+antd点击路由切换时出现闪屏

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

页面的效果

解决方案

完整代码

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 />}
] 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com