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

使用方式

相关推荐
二哈喇子!4 小时前
BOM模型
开发语言·前端·javascript·bom
二哈喇子!4 小时前
Vue2 监听器 watcher
前端·javascript·vue.js
摘星编程4 小时前
OpenHarmony + RN:Bluetooth连接蓝牙外设
react native·react.js·harmonyos
yanyu-yaya4 小时前
前端面试题
前端·面试·前端框架
二哈喇子!5 小时前
使用NVM下载Node.js管理多版本
前端·npm·node.js
GGGG寄了5 小时前
HTML——文本标签
开发语言·前端·html
摘星编程6 小时前
在OpenHarmony上用React Native:ActionSheet确认删除
javascript·react native·react.js
2501_944521596 小时前
Flutter for OpenHarmony 微动漫App实战:推荐动漫实现
android·开发语言·前端·javascript·flutter·ecmascript
Amumu121386 小时前
Vue核心(三)
前端·javascript·vue.js
CoCo的编程之路6 小时前
2026 前端效能革命:如何利用智能助手实现“光速”页面构建?深度横评
前端·人工智能·ai编程·comate·智能编程助手·文心快码baiducomate