axios 封装 对请求的封装在实际项目中是十分必要的,它可以让我们统一处理 http 请求。比如做一些拦截,处理一些错误等。本篇文章将详细介绍如何封装 axios 请求,具体实现的功能如下 基本配置 配置默认请求地址,超时等 请求拦截 拦截 request 请求,处理一些发送请求之前做的处理,譬如给 header 加 token 等 响应拦截 统一处理后端返回的错误 全局 loading 为所有请求加上全局 loading(可配置是否启用) 取消重复请求 当同样的请求还没返回结果再次请求直接取消 基础配置 这里以 vue3 为例,首先安装 axios,element-plus npm i axios element-plus 在 src 下新建 http/request.ts 目录用于写我们的封装逻辑,然后调用 aixos 的 create 方法写一些基本配置 import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios'; const service = axios.create({...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile