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

今天的分享到此结束,欢迎小伙伴们一起交流
边栏推荐
- C#通用接口调用
- 力扣------网格中的最小路径代价
- Three. JS local environment setup
- Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
- [pyg] understand the messagepassing process, GCN demo details
- MySql实战45讲【索引】
- QT based tensorrt accelerated yolov5
- 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)
- Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
- The difference between componentscan and componentscans
猜你喜欢

Check log4j problems using stain analysis

VS 2019 配置tensorRT生成engine
![MySQL practice 45 [global lock and table lock]](/img/23/fd58c185ae49ed6c04f1a696f10ff4.png)
MySQL practice 45 [global lock and table lock]

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

Three. JS local environment setup

The idea setting code is in UTF-8 idea Properties configuration file Chinese garbled

3D drawing example

On the adjacency matrix and adjacency table of graph storage

Le processus de connexion mysql avec docker

你真的懂继电器吗?
随机推荐
C#通用接口调用
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
Three.js本地环境搭建
3D drawing example
The XML file generated by labelimg is converted to VOC format
模型转换onnx2engine
I2C 子系统(四):I2C debug
Use of check boxes: select all, deselect all, and select some
Opengauss database development and debugging tool guide
将时间戳转为指定格式的时间
[shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling
I2C subsystem (IV): I2C debug
监听对象中值变化及访问
docker安装mysql
Agile certification (professional scrum Master) simulation exercise-2
I2C 子系統(四):I2C debug
Introduction to cron expression
I2C 子系统(三):I2C Driver
C # general interface call
销毁Session和清空指定的属性