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封装好的类

结果:

相关推荐
奔跑吧邓邓子23 分钟前
npm包管理深度探索:从基础到进阶全面教程!
前端·npm·node.js
前端李易安43 分钟前
ajax的原理,使用场景以及如何实现
前端·ajax·okhttp
汪子熙1 小时前
Angular 服务器端应用 ng-state tag 的作用介绍
前端·javascript·angular.js
Envyᥫᩣ1 小时前
《ASP.NET Web Forms 实现视频点赞功能的完整示例》
前端·asp.net·音视频·视频点赞
Мартин.5 小时前
[Meachines] [Easy] Sea WonderCMS-XSS-RCE+System Monitor 命令注入
前端·xss
昨天;明天。今天。7 小时前
案例-表白墙简单实现
前端·javascript·css
数云界7 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
风清扬_jd7 小时前
Chromium 如何定义一个chrome.settingsPrivate接口给前端调用c++
前端·c++·chrome
安冬的码畜日常7 小时前
【玩转 JS 函数式编程_006】2.2 小试牛刀:用函数式编程(FP)实现事件只触发一次
开发语言·前端·javascript·函数式编程·tdd·fp·jasmine
ChinaDragonDreamer7 小时前
Vite:为什么选 Vite
前端