axios 使用

1.官网-安装

http://www.axios-js.com/zh-cn/docs/

复制代码
npm install axios

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

TypeScript 复制代码
import axios from 'axios'
import { ElLoading } from 'element-plus'
import { ElMessage } from 'element-plus'
interface IResponseData<T> {
    code: number,
    message: string,
    data: T
}
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<IResponseData<T>> {
        return this.myAxios.get(url, { params, ...data });
    }

    post<T>(url: string, params?: object, data = {}): Promise<IResponseData<T>> {
        return this.myAxios.post(url, params, data);
    }

    put<T>(url: string, params?: object, data = {}): Promise<IResponseData<T>> {
        return this.myAxios.put(url, params, data);
    }

    delete<T>(url: string, params?: object, data = {}): Promise<IResponseData<T>> {
        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 文件中

TypeScript 复制代码
server: {

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

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

4.使用方式

TypeScript 复制代码
import http from "@/http/index";

onMounted(()=>{

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

        tableData.value = res.data;

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

})
相关推荐
老华带你飞37 分钟前
建筑材料管理|基于springboot 建筑材料管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring
半山烟雨半山青1 小时前
微信内容emoji表情包编辑器 + vue3 + ts + WrchatEmogi Editor
前端·javascript·vue.js
ss2732 小时前
SpringBoot+vue养老院运营管理系统
vue.js·spring boot·后端
用户841794814562 小时前
vue 甘特图 vxe-gantt table 依赖线的使用,配置连接线
vue.js
前端 贾公子2 小时前
Eruda:移动端网页调试利器
前端·javascript·vue.js
Hashan2 小时前
Elpis:抽离业务代码,发布NPM包
前端·javascript·vue.js
脾气有点小暴3 小时前
uniapp开发APP 内嵌外部 HTTPS 链接的实现方案
vue.js·uni-app
shuaijie05184 小时前
当表格数据量过大的时候,如何使用不分页进行展示
javascript·vue.js·ecmascript
JosieBook4 小时前
【Vue】03 Vue技术——Vue.js 入门学习笔记:Hello World 案例详解
vue.js·笔记·学习
lionliu05195 小时前
JavaScript 变量声明最佳实践
前端·javascript·vue.js