您的位置:首页 > 博客中心 > APP开发 >

react axios的二次封装

时间:2022-03-12 22:12

在src目录下面创建一个api文件夹 api里面创建index.js和request.js文件

index.js

import axios from "axios"
var service = axios.create({
    baseURL: "/api", //所有的请求都会带上 /api
    "content-type": "application/json",
    timeout: 5000
})
//请求拦截器
// service.interceptors.request.use((config)=>{
//     console.log("发请求了  以后要在这里带上令牌")
//     return config
// })

// 响应拦截器
// service.interceptors.response.use((rse) => {
//     return rse;
// })

export default service;

request.js

import axios from "./index"   // 请求

export const getList = () => {
    return axios.get("path")   //跨域要配置代理
}

组件如果想要调用,只需要引用 request.js 调用方法就好了。

相关推荐

电脑软件

本类排行

今日推荐

热门手游