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

})
相关推荐
Juchecar8 分钟前
TypeScript 中 JSON 对象加载与导出代码示例
javascript
Juchecar9 分钟前
JS前端为什么要打包工具,而打包工具为什么还那么慢?
javascript
·Alone15 分钟前
C++ list模拟实现
开发语言·c++
小高00722 分钟前
⚡90%前端没摸过的 10 个 JS 神 API?复制即用,今晚早下班
前端·javascript·面试
言兴26 分钟前
现代前端框架的响应式底层原理 —— 面试题从 Object.defineProperty 到 Proxy
前端·javascript·面试
PineappleCoder31 分钟前
红绿灯无限循环的 3 种实现,哪种更受面试官青睐?
前端·javascript·面试
杜子不疼.34 分钟前
《Python学习之基础语法2:掌握程序流程控制的艺术》
开发语言·python·学习
加油乐35 分钟前
设置动态页面标题/图标及解决favicon缓存更新问题
前端·javascript·vue.js
江城开朗的豌豆38 分钟前
React 开发小技巧:父组件如何‘操控’子组件的函数?
前端·javascript·react.js
C_Liu_1 小时前
C语言:队列的实现和剖析
c语言·开发语言·数据结构