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

})
相关推荐
single5941 分钟前
【优选算法】(第三十四篇)
java·开发语言·数据结构·c++·python·算法·leetcode
尹蓝锐3 分钟前
C语言-常见文件操作函数详解(fgetc,fputc,fgets,fputs,fscanf,fprintf,fread,fwrite)
c语言·开发语言
jjw_zyfx12 分钟前
vue2中 vue-count-to组件让数字从某个数字动态的显示到某个数字(后附vue3的用法)
javascript·vue.js·ecmascript
千里码aicood14 分钟前
[含文档+PPT+源码等]精品基于Python实现的Django高校职业通的设计与实现
开发语言·python·django
小林熬夜学编程31 分钟前
【Linux系统编程】第三十弹---软硬链接与动静态库的深入探索
服务器·c语言·开发语言·前端·c++·算法
ac-er888832 分钟前
PHP静态化和伪静态如何实现的
开发语言·php
tekin41 分钟前
php获取远程https内容时提示 PHP Warning: copy(): Unable to find the wrapper “https“ 解决方法
开发语言·macos·https·php
guangcheng0312q1 小时前
C++与Rust那些事之跳过析构函数
开发语言·c++·后端·rust
VaporGas1 小时前
SpringMVC Controller返回值技巧:ModelAndView vs String的实战对比
java·开发语言·前端·spring·springmvc·modelandview