当前位置: 首页 > article >正文

axios 使用

1.官网-安装

axios中文网|axios API 中文文档

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

www.axios-js.com

npm install axios

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


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 文件中

  server: {

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

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

4.使用方式

import http from "@/http/index";
onMounted(()=>{

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

        tableData.value = res.data;

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

})

http://www.kler.cn/news/342425.html

相关文章:

  • ECCV`24 | 新加坡国立华为提出Vista3D: 实现快速且多视角一致的3D生成
  • SCRM呼叫中心高保真Axure原型 源文件分享
  • 基于IOU匹配的DeepSort目标跟踪与匈牙利算法解析
  • QFontComboBox Class
  • 初识Linux操作系统与Shell命令
  • ARM base instruction -- umull
  • [vscode]格式化C#代码
  • 鸿蒙开发(NEXT/API 12)【HTTP数据请求】网络篇
  • oracle set命令
  • 澳鹏干货 | 大语言模型的上下文窗口 (Context Windows)
  • 使用 Go 语言与 Redis 构建高效缓存与消息队列系统
  • 六西格玛设计DFSS方法论在消费级无人机设计中的应用——张驰咨询
  • LangChain从模型中提取json输出
  • [含文档+PPT+源码等]精品基于Python实现的Django高校职业通的设计与实现
  • python sqlite3 工具函数
  • 【OpenCV】基础操作学习--实现原理理解
  • 论文阅读:OpenSTL: A Comprehensive Benchmark of Spatio-Temporal Predictive Learning
  • 如何提高LabVIEW编程效率
  • GitHub Copilot 使用手册(一)--配置
  • 雨晨 24H2 正式版 Windows 11 iot ltsc 2024 适度 26100.2033 VIP2IN1