当前位置:网站首页>loading加载和统一异常处理
loading加载和统一异常处理
2022-04-21 07:15:00 【cc&】
loading加载
1.按需导入 ElementUI 组件 Loading
2.加载数据时, 打开和关闭 对象 (注意: loadingInstance 实例采用单例模式创建,防止响应异常时频繁切换路由时加载动效重复创建)
3.请求拦截器中打开加载效果, 响应拦截器和异常中关闭加载效果.
统一异常处理
在响应拦截器中设置
加载和统一异常处理的完整代码:
import axios from 'axios';
import { Loading, Message } from 'element-ui';
const loading = {
loadingInstance: null, //loading实例
// 打开加载
open: function () {
// 创建实例,而且会加载窗口
if (this.loadingInstance === null) {
//采用单例
this.loadingInstance = Loading.service({
target: '.main',
text: '拼命加载中'
});
}
},
// 关闭加载
close: function () {
// 不为空时,则关闭加载窗口
if (this.loadingInstance !== null) {
this.loadingInstance.close();
}
this.loadingInstance = null;
}
};
const request = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 5000 //请求超时,5000毫秒
});
// 请求拦截器
request.interceptors.request.use(
(config) => {
// 请求拦截
// 打开加载窗口
loading.open();
return config;
},
(error) => {
// 关闭加载窗口
loading.close();
return Promise.reject(error);
}
);
// 响应拦截器
request.interceptors.response.use(
(response) => {
// 响应拦截
// 关闭加载窗口
loading.close();
const resp = response.data;
// 后台正常响应的状态,如果不是2000,说明后台处理有问题
if (resp.code !== 2000) {
Message({
message: resp.message || '系统异常',
type: 'warning',
duration: 5 * 1000
});
}
return response;
},
(error) => {
// 关闭加载窗口
loading.close();
//断网会出现这样的错误 |请求路径错误
// 当请求接口出错时, 进行弹出错误提示, 如 404, 500, 请求超时
console.log('response error', error.response.status)
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
});
return Promise.reject(error);
}
);
export default request;
版权声明
本文为[cc&]所创,转载请带上原文链接,感谢
https://blog.csdn.net/grow_/article/details/124303638
边栏推荐
- Pycharm's latest method of importing PIL Library
- Laravel print SQL
- VMware 16 newly installed win11 professional edition, unable to read the ISO image, unable to start the installer
- Valentina Studio Pro for Mac(mac数据库管理软件)
- Playwright click timeout attribute, the time to wait for the element to appear
- 数据库实验一、数据库的创建及基本查询
- Nail custom robot docking source code
- BurpSuite工具详解及暴库示例
- 亿级流量多级缓存架构
- Sword finger offer day22 bit operation (medium)
猜你喜欢

go语言记录日志uber-go/zap/lumberjack的用法

MongoDB 实验——数据备份和恢复和数据库优化

Ancient artifact VIM

2022G3锅炉水处理考试题库及答案

2022 R2 mobile pressure vessel filling test question simulation test question bank and simulation test
![[PROJECT] small hat takeout (VI)](/img/66/3ed0e0596abbf92e9111a1a0b7108c.png)
[PROJECT] small hat takeout (VI)

Yolov5 model environment construction and Google lab training

C51通过esp8266连接onenet(MQTT协议)上传温湿度+控制LED

matlab画散点图

从源码角度剖析redis分布式锁
随机推荐
Nail custom robot docking source code
【知行】浅谈Switch与If
强到离谱,Transformer为何能闯入CV界秒杀CNN?
天梯赛L3
The interface is not restored after Fiddler changes the font
[Ethernet switching security] - port security and MAC address drift prevention and detection
数学实验——函数绘图实验
sys.stdin.readline和readlines以及input()
Finds an array based on the value of a field in a two-dimensional array
Actual JDBC connection to MySQL database
Laravel packages multiple files and downloads them
Default password when installing Kali for virtual machine (VMX file source on official website)
C51通过esp8266连接onenet(MQTT协议)上传温湿度+控制LED
亿级流量多级缓存架构
Neptune w800点灯(中断)工程详解
Detailed explanation of classes and objects (detailed explanation of construction methods)
The listview column in C automatically adapts to the perfect effect of scaling
VMware提示恢复快照时出错找不到所需文件
It can switch the aircraft duel with three interfaces of start, battle and settlement, modularization, pyGame, rect and exe packaging
类中的泛型以及泛型类型