当前位置:网站首页>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’],需要拦截的都必须添加,如图
四、效果展示

今天的分享到此结束,欢迎小伙伴们一起交流
边栏推荐
- The XML file generated by labelimg is converted to VOC format
- Why does thread crash not cause JVM crash
- Last update time of all sqlserver tables
- The idea setting code is in UTF-8 idea Properties configuration file Chinese garbled
- 用docker 连接mysql的过程
- MySQL practice 45 lecture [transaction isolation]
- [C language] MD5 encryption for account password
- How to select the minimum and maximum values of columns in the data table- How to select min and max values of a column in a datatable?
- C language beginner level - pointer explanation - paoding jieniu chapter
- 基于Qt的yolov5工程
猜你喜欢

Segmentation fault occurs during VFORK execution
![MySQL practice 45 lecture [row lock]](/img/71/344daddee537a96f0d38241e6896e1.png)
MySQL practice 45 lecture [row lock]

Add automatic model generation function to hade

Spark on yarn resource optimization ideas notes

Yiwen takes you to know ZigBee

左连接,内连接

内存泄漏工具VLD安装及使用

I2C subsystem (II): I3C spec

VS 2019 配置tensorRT生成engine
![[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)](/img/19/815e7cba81f6eb52db5ef0db556dfd.jpg)
[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
随机推荐
C # general interface call
[C language] MD5 encryption for account password
Parameter index out of range (1 > number of parameters, which is 0)
Sous - système I2C (IV): débogage I2C
Are there any recommended term life insurance products? I want to buy a term life insurance.
PAT乙级“1104 天长地久”DFS优化思路
用docker 連接mysql的過程
Model transformation onnx2engine
File rename
From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character
[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
How to implement append in tensor
模糊查詢時報錯Parameter index out of range (1 > number of parameters, which is 0)
MySql实战45讲【索引】
docker安装mysql
Update and return document in mongodb - update and return document in mongodb
VS code配置虚拟环境
I2C subsystem (I): I2C spec
一文带你了解 ZigBee
Distributed transaction