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

使用方式

相关推荐
明天好,会的1 小时前
分形生成实验(五):人机协同破局--30万token揭示Actix-web状态管理的微妙边界
运维·服务器·前端
C_心欲无痕2 小时前
nginx - alias 和 root 的区别详解
运维·前端·nginx
我是苏苏4 小时前
Web开发:C#通过ProcessStartInfo动态调用执行Python脚本
java·服务器·前端
无羡仙4 小时前
Vue插槽
前端·vue.js
哈__4 小时前
React Native 鸿蒙跨平台开发:PixelRatio 像素适配
javascript·react native·react.js
用户6387994773055 小时前
每组件(Per-Component)与集中式(Centralized)i18n
前端·javascript
SsunmdayKT5 小时前
React + Ts eslint配置
前端
开始学java5 小时前
useEffect 空依赖 + 定时器 = 闭包陷阱?count 永远停在 1 的坑我踩透了
前端
zerosrat5 小时前
从零实现 React Native(2): 跨平台支持
前端·react native