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

使用方式

相关推荐
1024肥宅几秒前
手写 EventEmitter:深入理解发布订阅模式
前端·javascript·eventbus
海市公约1 小时前
HTML网页开发从入门到精通:从标签到表单的完整指南
前端·ide·vscode·程序人生·架构·前端框架·html
3秒一个大2 小时前
HTML5 与 JavaScript 中的二进制数据处理:ArrayBuffer 与 TextEncoder/Decoder 实践
javascript
purpleseashell_Lili2 小时前
如何学习 AG-UI 和 CopilotKit
javascript·typescript·react
行云流水6262 小时前
前端树形结构实现勾选,半勾选,取消勾选。
前端·算法
diudiu_332 小时前
web漏洞--认证缺陷
java·前端·网络
阿珊和她的猫3 小时前
<video>` 和 `<audio>` 标签的常用属性解析
前端
LSL666_3 小时前
4 jQuery、JavaScript 作用域、闭包与 DOM 事件绑定
前端·javascript·html
yinuo3 小时前
前端跨页面通讯终极指南⑤:window.name 用法全解析
前端
小飞侠在吗3 小时前
vue computed 和 watch
前端·javascript·vue.js