• vue3.0 axios封装


    环境: 使用vue3.0 及 axios,组件库使用element-plus的 ElMessage及ElLoading

    新建request.ts文件,专用于封装axios

    import axios, {
        AxiosResponse } from 'axios'
    import type {
        AxiosInstance, AxiosRequestConfig } from 'axios'
    import type {
       
      RequestConfig,
      RequestInterceptors,
      CancelRequestSource,
    } from './types'
    import {
        ElMessage, ElLoading } from "element-plus";
    let loadingInstance: any;
    const startLoading = () => {
       
      loadingInstance = ElLoading.service({
       
        lock: true,
        text: '加载中……',
        background: 'rgba(0, 0, 0, 0.7)'
      });
    };
    function endLoading() {
       
      loadingInstance.close();
    }
    class Request {
       
      // axios 实例
      instance: AxiosInstance
      // 拦截器对象
      interceptorsObj?: RequestInterceptors<AxiosResponse>
    
      /*
      存放取消方法的集合
      * 在创建请求后将取消请求方法 push 到该集合中
      * 封装一个方法,可以取消请求,传入 url: string|string[]  
      * 在请求之前判断同一URL是否存在,如果存在就取消请求
      */
      cancelRequestSourceList?: CancelRequestSource[]
      /*
      存放所有请求URL的集合
      * 请求之前需要将url push到该集合中
      * 请求完毕后将url从集合中删除
      * 添加在发送请求之前完成,删除在响应之后删除
      */
      requestUrlList?: string[]
    
      constructor(config: RequestConfig) {
       
        this.requestUrlList = []
        this.cancelRequestSourceList = []
        this.instance = axios.create(config)
        this.interceptorsObj = config.interceptors
        // 拦截器执行顺序 接口请求 -> 实例请求 -> 全局请求 -> 实例响应 -> 全局响应 -> 接口响应
        this.instance.interceptors.request.use(
          (res: AxiosRequestConfig) => res,
          (err: any) => err,
        )
    
        // 使用实例拦截器
        this.instance.interceptors.request.use(
          this.interceptorsObj?.requestInterceptors,
          this.interceptorsObj?.requestInterceptorsCatch,
        )
        this.instance.interceptors.response.use(
          this.interceptorsObj?.responseInterceptors,
          this.interceptorsObj?.responseInterceptorsCatch,
        )
        // 全局响应拦截器保证最后执行
        this.instance.interceptors.response.use(
          // 因为我们接口的数据都在res.data下,所以我们直接返回res.data
          (res: AxiosResponse) => {
       
            if(res.data.code == 200) {
       
              ElMessage({
       
                message: res.data.message,
                grouping: true,
                type: 'success',
              })
              return res.data
            } else {
       
              ElMessage.error(res.data)
              return res.data
            }
          },
          (err: any) => err,
        )
      }
      /**
       * @description: 获取指定 url 在 cancelRequestSourceList 中的索引
       * @param {string} url
       * @returns {number} 索引位置
       */
      private getSourceIndex(url: string): number {
       
        return this.cancelRequestSourceList?.findIndex(
          (item: CancelRequestSource) => {
       
            return Object.keys(item)[0] === url
          },
        ) as number
      }
      /**
       * @description: 删除 requestUrlList 和 cancelRequestSourceList
       * @param {string} url
       * @returns {*}
       */
      private delUrl(url: string) {
       
        const urlIndex = this.requestUrlList?.findIndex(u => u === url)
        const sourceIndex = this.getSourceIndex(url)
        // 删除url和cancel方法
        urlIndex !== -1 && this.requestUrlList?.splice(urlIndex as number, 1)
        sourceIndex !== -1 &&
          this.cancelRequestSourceList?.splice(sourceIndex as number, 1)
      }
      request<T>(config: RequestConfig<T>): Promise<T> {
       
        return new Promise((resolve, reject) => {
       
          startLoading()
          // 如果我们为单个请求设置拦截器,这里使用单个请求的拦截器
          if (config.interceptors?.requestInterceptors) {
       
            config = config.interceptors.requestInterceptors(config)
          }
          const url = config.url
          // url存在保存取消请求方法和当前请求url
          if (url) {
       
            this.requestUrlList?.push(url)
            // TODO 在axios0.22起,对CancelToken已经弃用,需要改成  AbortController 文档:https://axios-http.com/docs/cancellation
            config.cancelToken = new axios.CancelToken(c => {
       
              this.cancelRequestSourceList?.push
  • 相关阅读:
    高等数学(第七版)同济大学 习题10-2(中5题) 个人解答
    港股将迎“最牛十元店“,名创优品能借IPO突围?
    计算机毕业设计asp.net社团人员信息系统VS开发sqlserver数据库web结构c#编程计算机网页项目
    stable diffusion打造自己专属的LORA模型
    设计模式——迭代器模式(Iterator Pattern)+ Spring相关源码
    重学前端-js类型
    阿里云盘搜索引擎项目架构图
    1488. 避免洪水泛滥
    perspectiveTransform warpPerspective getPerspectiveTransform findHomography
    MyBatis入门
  • 原文地址:https://blog.csdn.net/qq_38074118/article/details/127118161