赞
踩
import axios, {AxiosInstance, AxiosResponse, InternalAxiosRequestConfig} from "axios"; // 统一请求路径前缀 let baseurl = '/miniapis'; const myAxios:AxiosInstance=axios.create({ timeout: 10000, // 设置请求超时时间(单位:毫秒) withCredentials: true, }) myAxios.interceptors.request.use((res:InternalAxiosRequestConfig):any=>{ res.headers['token']= window.localStorage.getItem('token')||'' return res }, (error):object=> { return Promise.reject(error); }); myAxios.interceptors.response.use( (response:AxiosResponse):any =>{ const data = response.data; console.log(response) if (response.status != 200) { return Promise.reject(data); } else { console.log('成功') return Promise.resolve(data); } }, (error):any =>{ return Promise.reject(error); }); // 封装get方法并导出 export function get(url: string, params?: object){ return myAxios.get(baseurl+url,{ params, }) } // 封装post方法 export function post(url: string, data?: object){ return myAxios.post(baseurl+url,data) } export default myAxios
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。