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

今天的分享到此结束,欢迎小伙伴们一起交流
边栏推荐
- MySql实战45讲【行锁】
- What happens between entering the URL and displaying the page?
- Andwhere multiple or query ORM conditions in yii2
- Process the dataset and use labelencoder to convert all IDs to start from 0
- 敏捷认证(Professional Scrum Master)模拟练习题-2
- MySQL practice 45 [SQL query and update execution process]
- [combinatorics] number of solutions of indefinite equations (number of combinations of multiple sets R | number of non negative integer solutions of indefinite equations | number of integer solutions
- From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character
- Docker install redis
- L'index des paramètres d'erreur est sorti de la plage pour les requêtes floues (1 > Nombre de paramètres, qui est 0)
猜你喜欢
随机推荐
How to use asp Net MVC identity 2 change password authentication- How To Change Password Validation in ASP. Net MVC Identity 2?
Idea set method call ignore case
Vs Code configure virtual environment
C语言初阶-指针详解-庖丁解牛篇
node 开启服务器
MySql实战45讲【索引】
yii2 中andWhere多个or查询 orm条件
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 do you adjust the scope of activerecord Association in rails 3- How do you scope ActiveRecord associations in Rails 3?
TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
VS 2019安装及配置opencv
I2C 子系统(三):I2C Driver
Chart. JS multitooltip tag - chart js multiTooltip labels
用docker 連接mysql的過程
Destroy the session and empty the specified attributes
vfork执行时出现Segmentation fault
@Accessors注解作用指定前缀遵守驼峰命名
Reset or clear NET MemoryStream - Reset or Clear . NET MemoryStream






![[pyg] understand the messagepassing process, GCN demo details](/img/8b/8490aac98fd2753e661f74e284f43d.png)
