import axios from "axios";
class AxiosRequest {
constructor(options = {}) {
const { baseURL = "https://example.com/api", timeout = 10000, headers = {} } = options;
this.instance = axios.create({
baseURL,
timeout,
headers: {
"Content-Type": "application/json;charset=utf-8",
...headers // 将传入的 headers 与默认 headers 合并
}
});
}
get(url, params = {}, config = {}) {
return this.instance.get(url, {
params,
...config
});
}
post(url, data = {}, config = {}) {
return this.instance.post(url, data, config);
}
}
const axiosRequest = new AxiosRequest({ headers: { Authorization: "Bearer mytoken123" } });
export default axiosRequest;
使用class封装axios请求
最新推荐文章于 2023-11-25 09:00:00 发布