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

使用方式

相关推荐
计算机魔术师11 分钟前
两年翻45倍!英伟达靠买买买建成千亿投资帝国
前端
paopaokaka_luck14 分钟前
基于springboot3+vue3的精准扶贫管理系统(AI 问答、ECharts 图形化分析)
前端·人工智能·echarts
l1m0_31 分钟前
智能电动自行车管理后台实战:AI生成页面与React组件化技巧
前端·react.js·ui·ai·设计
Patrick_Wilson1 小时前
为什么gitlab的MR会默认有一个merge commit
前端·git·gitlab
坚果的博客1 小时前
认识 CPF-RN:React Native 鸿蒙官方社区与四大核心仓库
react native·react.js·harmonyos
en.en..1 小时前
Linux mmap 内存映射深度解析:基于帧缓冲 /dev/fb0
java·服务器·前端
后台模板学习2 小时前
从0到1用Vite构建电商订单系统前端性能优化方案
前端
Wang's Blog2 小时前
Vibe Coding一人即团队系列58: HTML演示文稿自动生成
前端·人工智能·html
FoldWinCard2 小时前
基于k8s高可用web集群
前端·容器·kubernetes
神探小白牙2 小时前
海康视频在vue2.0中的使用
前端·音视频