axios的使用,处理请求和响应,axios拦截器

1、axios官网

https://www.axios-http.cn/docs/interceptors

2、安装

npm install axios

3、在onMouunted钩子函数中使用axios来发送请求,接受响应

4.出现的问题:

(1) 但是如果发送请求请求时间过长,回出现请求待处理的情况,用户体验很不好,没有画面,我们可以加一个loding遮罩层,提示用户正在加载中,但是如果没个请求都手动添加,代码冗余

(2) 每个请求都要考虑,程序报错的情况,都需要catch一下,处理下异常,而且在拿数据时我们后端写了统一返回格式,但是前端响应的数据res里我们的数据被一层一层包裹着,每次都要一层一层的拿,代码冗余

5、解决方法:

使用axios的拦截器

新建一个http文件夹,新建index.ts文件用于定义请求和响应拦截器,在请求和响应拦截器中解决以上问题

复制代码
import axios from 'axios'
import { ElMessage, ElLoading } from 'element-plus'
const config = {
    baseURL: '',
    timeout: 30 * 1000,
    withCredentials: true,
}

let loading: any
class Http {
    myAxios: any;
    constructor(config: any) {
        this.myAxios = axios.create(config);
        // 添加请求拦截器
        this.myAxios.interceptors.request.use(function (config: any) {
            //在发送请求时加载loading层
            loading = ElLoading.service({
                lock: true,
                text: '加载中...',
                background: 'rgba(0, 0, 0, 0.7)',
            })

            return config;

        }, function (error: any) {
            // 对请求错误做些什么
            loading.close()
            return Promise.reject(error);
        });
        // 添加响应拦截器
        this.myAxios.interceptors.response.use(function (response: any) {
            //在响应后关闭loading层
            loading.close()
            //取出响应的数据进行判断
            const { code, message, data } = response.data

            if (code == 0) {
                return data
            } else if (code == undefined) {
                return response
            } else {

                ElMessage.error(message)
                return Promise.reject(message);
            }


        }, function (error: any) {
            loading.close()
            return Promise.reject(error);
        });
    }
    get<T>(url: string, params?: object, data = {}): Promise<any> {
        return this.myAxios.get(url, { params, ...data });
    }

    post<T>(url: string, params?: object, data = {}): Promise<any> {
        return this.myAxios.post(url, params, data);
    }

    put<T>(url: string, params?: object, data = {}): Promise<any> {
        return this.myAxios.put(url, params, data);
    }

    delete<T>(url: string, params?: object, data = {}): Promise<any> {
        return this.myAxios.delete(url, { params, ...data });
    }

}


export default new Http(config);

在页面中使用时,直接使用用axios封装好的类

结果:

相关推荐
IMPYLH6 分钟前
HTML 的 <ol> 元素
前端·html
IMPYLH6 分钟前
HTML 的 <object> 元素
前端·html
DevOpenClub8 分钟前
PDF 多格式解析如何避免混用输出:TEXT、HTML、XML 与 TAG 数据契约
xml·前端·数据库·pdf·html
用户2526327534717 分钟前
请问同一台服务器能同时部署前后端项目并支持https吗?
前端
Sincerelyplz30 分钟前
【Pipecat】基于Pipecat的voice agent实践
前端·后端·agent
lerhxx33 分钟前
为什么你的Three.js物体总是乱转?一文彻底搞懂“万向锁”与四元数
前端·three.js
计算机魔术师43 分钟前
写了代码还要人修?Google 的 AI 编程助手自己打补丁了
前端
DevOpenClub1 小时前
Markdown、HTML 和 PPT 如何稳定交付:文档转换任务的幂等发布流程
开发语言·前端·c#·html·powerpoint
DevOpenClub2 小时前
网页证据如何长期复核:快照、静态 HTML 与 PDF 归档链路
前端·pdf·html
Canace2 小时前
基于 Codex Agent Harness 套壳实现自己的 AI 产品:Agent 运行时与任务编排实践
前端·人工智能·agent