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

使用方式

相关推荐
IT_陈寒1 小时前
React的这个渲染问题连官方文档都没说清楚
前端·人工智能·后端
追逐时光者2 小时前
别再满网找零散工具了,腾讯 QQ 浏览器这个“帮小忙”工具箱真能省时间
前端·后端
To_OC4 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
Asmewill4 小时前
grep&curl命令学习笔记
前端
stringwu4 小时前
Flutter 开发必备:MVI 架构的高效实现指南
前端·flutter
用户2136610035725 小时前
Vue2组件化开发与父子通信
前端·vue.js
Momo__6 小时前
TypeScript satisfies 操作符——比 as 更安全的类型守门员
前端·typescript
用户2136610035726 小时前
Vue2事件系统与指令进阶
前端·vue.js
labixiong6 小时前
实现一个能跑的迷你版Promise(一)
前端·javascript·面试
Csvn8 小时前
`??` 和 `||` 搞混,线上用户头像全挂了
前端