vue3+ts封装axios 配置BASE_URL拦截器 单个接口的拦截 全局拦截器

news2024/9/25 9:32:29

在这里插入图片描述

1. config.ts 书写BASE_URL ( service/request/config.ts)

BASE_URL书写的方式很多
1: 直接在axios.create里面写死 ,在打包或者测试的时候手动进行修改BASE_URL
2:新建一个文件 在文件里面判断当前的环境 进行赋值BASE_URL
3:定义.env文件 Eg: env.development ;env.production; env.test 在文件里面进行书写变量 Eg:VUE_APP_BASE_URL=‘XXX’

我们选择新建文件config.ts

// config.ts定义环境变量
let BASE_URL: string
const TIME_OUT = 10000

if (process.env.NODE_ENV == 'development') {
    BASE_URL = "https://v.api.aa1.cn/api"
} else if (process.env.NODE_ENV == 'production') {
    BASE_URL = "http://152.136.185.210:5000"

} else {
    // test测试环境
    BASE_URL = "http://152.136.185.210:5000"


}
export { BASE_URL, TIME_OUT } 

2.创建axios --> 配置全局拦截,BASE_URL请求和响应拦截, 单个接口的请求和响应拦截 (service/request/index.ts)

	我们会发现我们的项目有的不止是请求的一个BASE_URL,这时候我们会创建个axios比较臃肿,我们可以使用ES6的语法进行创建class类 
	写一个函数体 进行new出子类创建对象即可

防止大家看不懂下面的逻辑 在这里写一个小demo

 interface obj {
    name: string;
    age: number
}
interface obj1 extends obj {
    boxobj: obj
}
class person {
    name: string;
    age: number;
    boxobj: object;
    constructor(obj: obj1) {
        this.age = obj.age;
        this.name = obj.name;
        this.boxobj = obj.boxobj
    }
}
console.log(new person({ age: 11, name: 'zz', boxobj: { name: 'zz', age: 232 } }))

正式创建

// 定义class对象的
import axios from 'axios';
import type { AxiosInstance } from 'axios';
import type { HYRequestInterceptors, HYRequestConfig } from './type' 
class HYRequest {
    instance: AxiosInstance;
    interceptors?: HYRequestInterceptors;//拦截器
    constructor(config: HYRequestConfig) {
        // config// 传输的base_url  timeout 拦截器
        // const servise = axios.create(config)
        // servise.interceptors.request.use
        this.instance = axios.create(config)
        this.interceptors = config.interceptors;//传输的是对象
        // 针对base_url的拦截
        this.instance.interceptors.request.use(this.interceptors?.requestInterceptor, this.interceptors?.requestInterceptorCatch)
        this.instance.interceptors.response.use(this.interceptors?.responseInterceptor, this.interceptors?.responseInterceptorCatch)
        // 全局的拦截
        this.instance.interceptors.request.use((config) => { return config }, (err) => { return err })
        this.instance.interceptors.response.use((config) => { return config }, (err) => { return err })
    }
    // 请求request
    // config调用这个request函数的时候传输的config(单个接口拦截器)
    request<T = any>(config: HYRequestConfig): Promise<T> {
        return new Promise((resolve, reject) => {
            // 请求拦截  如果需要对当前的某个接口进行拦截 那么调用这个传输过来的方法 进行操作完毕再把结果返回过来 在重新赋值config
            if (config.interceptors?.requestInterceptor) {
                config = config.interceptors.requestInterceptor(config)
            }
            // 发送请求
            this.instance.request<any, T>(config).then((res) => {
                // 对单个请求的响应拦截进行操作
                if (config.interceptors?.responseInterceptor) {
                    // 把获取的结果传输给这个拦截进行操作在返回给我 我在resolve出去
                    res = config.interceptors.responseInterceptor(res)
                }
                resolve(res)
            }).catch((err) => {
                reject(err)
                return err
            })
        })
    }
    // T  限制的是返回的结果 
    get<T = any>(config: HYRequestConfig<T>): Promise<T> {
        return this.request<T>({ ...config, method: 'GET' })
    }

    post<T = any>(config: HYRequestConfig<T>): Promise<T> {
        return this.request<T>({ ...config, method: 'POST' })
    }

    delete<T = any>(config: HYRequestConfig<T>): Promise<T> {
        return this.request<T>({ ...config, method: 'DELETE' })
    }

    patch<T = any>(config: HYRequestConfig<T>): Promise<T> {
        return this.request<T>({ ...config, method: 'PATCH' })
    }


}
export default HYRequest

3.创建针对上面service/request/index.ts文件的类型限制

新建 (service/request/type.ts)

// new  class对象导出外部使用的
import type { AxiosRequestConfig, AxiosResponse } from 'axios'
// 定义可能是base_url传输的针对接口的拦截器  为变量做类型限制的
export interface HYRequestInterceptors<T = AxiosResponse> {
    // requestInterceptor?: (config: AxiosRequestConfig) => AxiosRequestConfig;
    requestInterceptor?: (config: any) => any;
    requestInterceptorCatch?: (error: any) => any
    // responseInterceptor?: (res: T) => T
    responseInterceptor?: (res: any) => any
    responseInterceptorCatch?: (error: any) => any

}
export interface HYRequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
    interceptors?: HYRequestInterceptors<T>
    showLoading?: boolean

}

4.new出class实例对象 就可以使用了 新建 service/index.ts文件

import HYRequest from './request/index'
import { BASE_URL, TIME_OUT } from './request/config'

// 地址1 
const hyRequest = new HYRequest({
    baseURL: BASE_URL,
    timeout: TIME_OUT,
    interceptors: {
        requestInterceptor: (config) => {
            // 携带token的拦截 
            return config
        },
        requestInterceptorCatch: (err) => {
            return err
        },
        responseInterceptor: (res) => {
            return res
        },
        responseInterceptorCatch: (err) => {
            return err
        }
    }
})
export default hyRequest
// 地址2  多个BASE_URL的时候 直接new以下class即可
// const hyRequest2 = new HYRequest({
//     baseURL: BASE_URL,
//     timeout: TIME_OUT,
//     interceptors: {
//         requestInterceptor: (config) => {
//             // 携带token的拦截

//             return config
//         },
//         requestInterceptorCatch: (err) => {
//             return err
//         },
//         responseInterceptor: (res) => {
//             return res
//         },
//         responseInterceptorCatch: (err) => {
//             return err
//         }
//     }
// })

上面的实例对象下不写interceptors 也可以 开启全局拦截照样使用

5.创建 service/types.ts类型文件 主要作用是限制接口返回参数的类型

// 限制的是返回的结果 T是返回的data的数据  不传默认是any
export interface IDataType<T = any> {
    code: number;
    data: T
}
// 测试接口返回的数据类型
export interface IStatusDataType<T = any> {
    status: number;
    data: T
}

6.新建api接口 service/login/login.ts

import hyReauest from '../index'//service/index
import { IAccount, ILoginResult, IStatusDataResul } from './types'
import { IDataType, IStatusDataType } from '../types'
enum LoginApi {
    AccountLogin = '/login',
    ceshis = "qqjson/index.php"
}
// account: IAccount  参数的类型限制
export function accountLoginRequest(account: IAccount) {
    return hyReauest.post<IDataType<ILoginResult>>({
        url: LoginApi.AccountLogin,
        data: account
    })
}
// 免费api接口测试
export function ceshiget() {
    return hyReauest.get<IStatusDataType<IStatusDataResul>>({
        url: LoginApi.ceshis,
        params: {
            qq: 'xxxxx'
        }
    })
}

7.定义针对login.ts的类型文件

// 针对login的类型限制
export interface IAccount {
    username: string;
    pwd: string;
}
export interface ILoginResult {
    id: number;
    name: string;
    token: string
}
//这个是演示接口
export interface IStatusDataResul {
    nickname: string;
    email: string;
    touxiang: string;
}

6. vue文件正式调用接口

<script  lang="ts"  setup>
import { accountLoginRequest, ceshiget } from '@/service/login/login'
 ceshiget().then((res) => {
      console.log(res, '请求到数据')
    })
</script>

7.成功获取数据

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/657705.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

线程间同步

线程间资源竞争 int count 0;void * add(void *arg){int val,i;for(i 0;i< 5000;i ){val count;printf("%p: %d\n",pthread_self(),val);count val 1;}return nullptr; }int main(){pthread_t tida,tidb;pthread_create(&tida,NULL,add,NULL);pthread_c…

python爬虫进行AES解密遇到的问题

1、TypeError: Object type <class ‘str’> cannot be passed to C code 报错如下&#xff1a; File "C:\Python311\Lib\site-packages\Crypto\Util\_raw_api.py", line 143, in c_uint8_ptrraise TypeError("Object type %s cannot be passed to C cod…

【改进算法】混合鲸鱼WOA和BAT算法(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

0101B站学习视频发留言找小伙伴-实用小工具系列

文章目录 1 起因2 找方法3 bilibili_api4 实现5 知识点结语 1 起因 经常在B站看学习视频&#xff0c;但是一个人学习&#xff0c;偶尔在想&#xff0c;我学的怎么样&#xff1f;有没有用&#xff1f;有没有谁可以一起交流下&#xff1f;好在现在有互联网&#xff0c;可以极大的…

WiFi各协议理论速度

一、总览 二、11b到11g提升点 802.11g工作在2.4G频段下&#xff0c;能够支持OFDM和CCK两种调制方式&#xff0c;提供16-QAM、64-QAM、BPSK和QPSK四种编码方式&#xff0c;我们通常说的54Mbps速率就是在2.4G频段下&#xff0c;通过OFDM调制&#xff0c;采用64-QAM编码的情况下实…

表达式和语句

表达式 可以被求值的代码&#xff0c;并将其计算出一个结果 语句 一段可以执行的代码&#xff0c;是一个行为&#xff0c;例如分支语句和循环语句 三大流程控制语句 以前写的代码&#xff0c;写几句就从上往下执行 &#xff0c;---顺序结构 有时候要根据条件 选择执行代码…

Spring源码之PostProcessor解析

系列文章目录 文章目录 系列文章目录前言一、PostProcessor是什么二、PostProcessor的作用三、Spring框架中有哪些PostProcessor呢BeanPostProcessorBeanFactoryPostProcessorInstantiationAwareBeanPostProcessorDestructionAwareBeanPostProcessorMergedBeanDefinitionPostPr…

Pinia 和 Vuex ,理解这两个 Vue 状态管理模式

Pinia和Vuex一样都是是vue的全局状态管理器。其实Pinia就是Vuex5&#xff0c;只不过为了尊重原作者的贡献就沿用了这个看起来很甜的名字Pinia。 本文通过Vue3的形式对两者的不同实现方式进行对比&#xff0c;让你在以后工作中无论使用到Pinia还是Vuex的时候都能够游刃有余。 …

Linux下在日志中打印时间戳

1、背景介绍&#xff1a;在实验过程中需要记录任务运行情况&#xff0c;为此需要在日志中增加时间戳打印信息&#xff0c;方便事后查看。 2、实现方法 示例如下&#xff1a; #include <stdio.h> #include <time.h> #include<string.h>void print_debug_me…

如何在iPhone上用ChatGPT替换Siri

To use ChatGPT with Siri on an iPhone or iPad, get an OpenAI API key and download the ChatGPT Siri shortcut. Enter your API key in the shortcut setup and select the GPT model you want to use, then hit “Add Shortcut.” Trigger the shortcut manually first t…

FreeRTOS实时操作系统(二)系统文件代码学习

文章目录 前言系统配置任务创建任务创建删除实践 前言 接着学习正点原子的FreeRTOS教程&#xff0c;涉及到一些详细的系统内文件代码 系统配置 可以通过各种的宏定义来实现我们自己的RTOS配置&#xff08;在FreeRTOSconfig.h&#xff09; “INCLUDE”&#xff1a;配置API函数…

【Java】catch里面抛出了异常finally里面的事务会提交吗?

文章目录 背景目前的代码直接实战演示单元测试总结 背景 我们公司的系统中有一个业务场景&#xff0c;需要第三方的账户数据同步到我们系统。 同步账号的同时&#xff0c;会将所有同步数据和是否成功记录到一张同步日志表中&#xff0c;方便排查问题和记录。 好了&#xff0c;…

window11系统CUDA、cuDNN 安装以及环境变量配置

文章目录 一&#xff0c;说明二&#xff0c;cuda的下载以及安装1. 确定自己电脑设备哪个版本cudaa. 点击左下角b. 点击左下角c.接着点击 组件 2. cuda的下载3. cuda的安装1. 双击 点击 ok2. 同意即可3. 这个随意哪个都行4.选择安装位置 接着下一步 三&#xff0c;cuda环境变量设…

Oracle安装时先决条件检查失败和[INS-35180] 无法检查可用内存问题解决

Oracle安装时先决条件检查失败和[INS-35180] 无法检查可用内存问题解决 问题&#xff1a; [INS-13001] 此操作系统不支持 Oracle 数据库问题原因解决方案 问题2&#xff1a;[INS-35180] 无法检查可用内存问题原因解决方案 问题&#xff1a; [INS-13001] 此操作系统不支持 Oracl…

Python面向对象编程-构建游戏和GUI 手把手项目教学(1.1)

总项目目标&#xff1a;设计一个简单的纸牌游戏程序&#xff0c;称为"Higher or Lower"&#xff08;高还是低&#xff09;。游戏中&#xff0c;玩家需要猜测接下来的一张牌是比当前牌高还是低。根据猜测的准确性&#xff0c;玩家可以得到或失去相应的积分。 项目1.1…

循环码的编码、译码与循环冗余校验

本专栏包含信息论与编码的核心知识&#xff0c;按知识点组织&#xff0c;可作为教学或学习的参考。markdown版本已归档至【Github仓库&#xff1a;https://github.com/timerring/information-theory 】或者公众号【AIShareLab】回复 信息论 获取。 文章目录 循环码的编码循环码…

实现 strStr

在一个串中查找是否出现过另一个串&#xff0c;这是KMP的看家本领。 28. 实现 strStr() 力扣题目链接 实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串&#xff0c;在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在&…

七、docker-compose方式运行Jenkins,更新Jenkins版本,添加npm node环境

docker-compose方式运行Jenkins&#xff0c;更新Jenkins版本&#xff0c;添加npm node环境 一、docker-compose方式安装运行Jenkins 中发现Jenkins版本有点老&#xff0c;没有node环境&#xff0c;本节来说下更新jenkins 及添加构建前端的node环境。 1. 准备好docker-compose…

算法刷题-双指针-二分法

27. 移除元素 力扣题目链接 给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素&#xff0c;并返回移除后数组的新长度。 不要使用额外的数组空间&#xff0c;你必须仅使用 O(1) 额外空间并原地修改输入数组。 元素的顺序可以改变。你不需…

XSS数据接收平台——蓝莲花(BlueLotus)

文章目录 一、前言二、安装三、使用1、我的JS创建一个模板2、使用创建的模板攻击3、打开攻击的目标&#xff08;这里选择pikachu靶场的存储型XSS模块测试&#xff09;4、查看返回的数据 一、前言 蓝莲花平台是清华大学曾经的蓝莲花战队搭建的平台&#xff0c;该平台用于接收xs…