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

使用方式

相关推荐
Htr_1 分钟前
Vercel 使用指南:框架、工作流与基础设施一体化的现代 Web 部署平台
前端
一颗烂土豆14 分钟前
ECharts 太平面?试试这款 Vue 3D 图表库
前端·vue.js·echarts
anyup23 分钟前
迁移uni-app x,我是如何让 AI 把我一步步搞崩溃的...
前端·uni-app·trae
用户9210802628631 分钟前
从读框架到搭项目:基于 Ant Design X Vue 和 RICH 范式搭建 AI 前端工作台
前端
Hilaku32 分钟前
为什么同一段代码在 Safari 上永远有 Bug?
前端·javascript·程序员
半仙er37 分钟前
第二周06天 Vue3 + TypeScript 实战与本周复盘
前端
heyCHEEMS39 分钟前
切页回来组件消失了?一个浏览器渲染机制引起的容器高度坍塌 bug
前端·浏览器
张元清40 分钟前
React scrollIntoView + useRef:滚动到指定元素 (2026)
javascript·react.js
iaku40 分钟前
Prompt 不是玄学:写给前端的 Prompt 工程指南
前端·人工智能
爱丶不疚40 分钟前
在 dsh 仓库里扒到的宝藏工作流:详解 .agents/notes 决策沉淀系统
前端·agent·vibecoding