axios 使用

1.官网-安装

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

npm install axios

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

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

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

})

相关推荐

  1. axios 使用

    2024-02-22 10:32:02       32 阅读
  2. Axios使用

    2024-02-22 10:32:02       12 阅读
  3. axios使用

    2024-02-22 10:32:02       37 阅读
  4. vue axios 使用

    2024-02-22 10:32:02       37 阅读
  5. axios介绍和使用

    2024-02-22 10:32:02       24 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-22 10:32:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-22 10:32:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-22 10:32:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-22 10:32:02       20 阅读

热门阅读

  1. Linux系统安装KafKa

    2024-02-22 10:32:02       32 阅读
  2. 通过例子学习golang的Goroutine

    2024-02-22 10:32:02       29 阅读
  3. 【LeetCode-198】打家劫舍(回溯&动归)

    2024-02-22 10:32:02       32 阅读
  4. ubuntu 学习

    2024-02-22 10:32:02       33 阅读
  5. VSCode使用教程

    2024-02-22 10:32:02       34 阅读
  6. 虚拟地址空间与堆区

    2024-02-22 10:32:02       31 阅读
  7. hive rlike

    2024-02-22 10:32:02       26 阅读
  8. Hive 最全面试题及答案(基础篇)

    2024-02-22 10:32:02       26 阅读