当前位置:网站首页>umi 路由拦截(简单粗暴)
umi 路由拦截(简单粗暴)
2022-07-03 03:12:00 【拾忆@y】
一、业务场景:
最近在使用umi进行开发,其中在做导航路由渲染的时候遇到了一些坑,为了大家后面遇到和我一样的问题,给大家分享一下
二、解决方案:
先添加一个高阶组件,跟所有组件同级,再配置路由的地方添加配置 wrappers: [‘@/wrappers/auth’],怕大家不理解,下面有图。
三、具体实现步骤
1、在src的目录下,和page同级,新建一个wrappers文件夹(这个名称是固定的),在文件夹下新建 Auth.tsx文件,
2、在文件里面的写入以下内容:
import {Redirect} from 'umi'
export default function Auth(props:any) {
//获取本地缓存 localStorage的值
const isLogin = localStorage.getItem('role')
//如果这个值存在就返回正常内容
if(isLogin){
return (
<div>
{props.children}
</div>
);
}else {
//如果这个值存在就重定向到登录页
console.log('跳登录')
return ( <Redirect to='/login' />)
}
}
3、在配置路由的地方加上 wrappers: [‘@/wrappers/auth’],需要拦截的都必须添加,如图
四、效果展示

今天的分享到此结束,欢迎小伙伴们一起交流
边栏推荐
- QT based tensorrt accelerated yolov5
- Variable declarations following if statements
- Three.js本地环境搭建
- Do you really understand relays?
- [error record] the parameter 'can't have a value of' null 'because of its type, but the im
- Practice of traffic recording and playback in vivo
- LVGL使用心得
- Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
- vfork执行时出现Segmentation fault
- MySql實戰45講【SQL查詢和更新執行流程】
猜你喜欢
![MySQL Real combat 45 [SQL query and Update Execution Process]](/img/cd/3a635f0c3bb4ac3c8241cb77285cc8.png)
MySQL Real combat 45 [SQL query and Update Execution Process]

Left connection, inner connection
![MySQL practice 45 [SQL query and update execution process]](/img/cd/3a635f0c3bb4ac3c8241cb77285cc8.png)
MySQL practice 45 [SQL query and update execution process]

Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)

Installation and use of memory leak tool VLD

Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes

Spark on yarn资源优化思路笔记
![[error record] the parameter 'can't have a value of' null 'because of its type, but the im](/img/1c/46d951e2d0193999f35f14d18a2de0.jpg)
[error record] the parameter 'can't have a value of' null 'because of its type, but the im

VS code配置虚拟环境

Le processus de connexion mysql avec docker
随机推荐
Reset or clear NET MemoryStream - Reset or Clear . NET MemoryStream
销毁Session和清空指定的属性
Gavin teacher's perception of transformer live class - rasa project's actual banking financial BOT Intelligent Business Dialogue robot architecture, process and phenomenon decryption through rasa inte
How to make backgroundworker return an object
About HTTP cache control
Find the storage address of the elements in the two-dimensional array
分布式事务
监听对象中值变化及访问
Vs Code configure virtual environment
Pytorch配置
Can I use read-only to automatically implement properties- Is read-only auto-implemented property possible?
将时间戳转为指定格式的时间
I2C 子系统(四):I2C debug
I2C 子系统(三):I2C Driver
Chart. JS multitooltip tag - chart js multiTooltip labels
The process of connecting MySQL with docker
C语言初阶-指针详解-庖丁解牛篇
idea 加载不了应用市场解决办法(亲测)
I2C 子系统(二):I3C spec
The idea setting code is in UTF-8 idea Properties configuration file Chinese garbled