react 路由权限

TypeScript 复制代码
import {  Spin } from "antd";
import { Suspense } from "react";
import { Navigate } from "react-router-dom";
import KeepAlive from 'react-activation'
function AuthRoute(props: any) {
    const spinStyle = {
        width: '100%',
        height: '100%',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center'
    }
    const token = localStorage.getItem("token");

    let template = <Navigate to='/' replace></Navigate>;

    if (token || !props.auth) {
        if (props.id) {
            template = <KeepAlive autoFreeze={false} when={true} key={props.id} id={props.id}>
                <Suspense fallback={<div style={spinStyle}><Spin /></div>}>{props.children}</Suspense>
            </KeepAlive>
        } else {
            template = <Suspense fallback={<div style={spinStyle}><Spin /></div>} >{props.children}</Suspense>
        }

    }
    return template
}
export default AuthRoute

使用方式

相关推荐
Csvn10 小时前
Monorepo 迁移血泪史:从 Multi-Repo 到 Turborepo,这 3 个坑我帮你踩完了
前端
星栈10 小时前
Dioxus 多页面怎么做:`dioxus-router`、嵌套路由、`Outlet` 和页面组织,一篇给你讲顺
前端·rust·前端框架
用户9874092388710 小时前
用 Remotion + edge-tts 打造中文教学视频全自动流水线
前端
风骏时光牛马10 小时前
Less前端工程化实战:变量混合器与项目样式分层落地
前端
假如让我当三天老蒯10 小时前
Options API(选项式 API) 和 Composition API(组合式 API)
前端·vue.js·面试
SameX10 小时前
iOS 独立开发实践:用 MapKit + 像素渲染实现 Citywalk 轨迹地图 App「雁过留痕」
前端
_柳青杨10 小时前
一文吃透 Node.js 事件循环:从原理到 Node 20+ 重大变更
javascript·后端
spmcor10 小时前
React 进阶指南:状态管理进化——从 Context 到 Redux Toolkit(第五篇)
react.js
spmcor10 小时前
React 进阶指南:React Router v6 完全实战(第四篇)
react.js
skyey10 小时前
页面加载时,深色模式闪白的问题解决
前端