axios 使用

1.官网-安装

axios中文网|axios API 中文文档

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。

www.axios-js.com

复制代码
npm install axios

2.统一封装类,新建src/http/index.ts

javascript 复制代码
import axios from 'axios'
import { ElLoading,ElMessage  } from 'element-plus'


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: 'Loading',
                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,msg,data} = response.data

           if(code === 0){
             return data;
           } else if (code == undefined){
            return response;
           } else if(code != 0){
             ElMessage.error(msg)
             return Promise.reject(msg);
           }

            
        }, 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 });
    }

}
const config = {
    baseURL: '',
    timeout: 30 * 1000,
    withCredentials: true,
}

export default new Http(config);
 

3..Proxy配置

在vite.config.ts 文件中

javascript 复制代码
  server: {

    port: 5000, // 你需要定义的端口号

    proxy: {
      "/api": {
        target: "Api地址",
        changeOrigin: true,
     
      },
    
    },
 },

4.使用方式

javascript 复制代码
import http from "@/http/index";
onMounted(()=>{

 http.get(
        "/api/products"
      )
      .then((res: any) => {

        tableData.value = res.data;

      })
      .catch((err: any) => {
        console.log(err);
      });

})
相关推荐
zhengfei6112 分钟前
第2章 Agent 核心组件深度解析
前端·javascript·react.js
say_fall7 分钟前
可编程中断控制器8259A工作方式超详细解析
android·开发语言·学习·硬件架构·硬件工程
San813_LDD11 分钟前
[QT]《Qt 开发避坑指南:随机数、容器操作与 VS 环境配置》
开发语言·qt
GuWen_yue14 分钟前
LeetCode 76 最小覆盖子串|JS 滑动窗口标准解法(逐行精讲)
javascript·算法·leetcode
小糯米60114 分钟前
C语言 自定义类型:联合和枚举
java·c语言·开发语言
weixin_5231853217 分钟前
Java基础知识总结(二):JVM内存结构与变量生命周期
java·开发语言·jvm
石山代码26 分钟前
Python 进阶学习指南
开发语言·python
万少1 小时前
产品原型不用从零画 -GPT 出图,Gemini 生成 HTML
前端·javascript·后端
xiaoshuaishuai81 小时前
C# 多线程之间对比
java·开发语言·c#
ZC跨境爬虫2 小时前
跟着 MDN 学JavaScript day_9:字符串方法实战挑战与解题思路
开发语言·前端·javascript