uniapp 封装axios
注:axios必须低于0.26.0,重中之重
重点:封装axios的适配器adapter
1.安装axios
npm install axios@0.26.0
创建api文件夹
2.新建adapter.js文件
import settle from "axios/lib/core/settle"
import buildURL from "axios/lib/helpers/buildURL"
import buildFullPath from "axios/lib/core/buildFullPath"
const adapter = function(config) {
return new Promise((resolve, reject) => {
let fullurl = buildFullPath(config.baseURL, config.url)
uni.request({
method: config.method.toUpperCase(),
url: buildURL(fullurl, config.params, config.paramsSerializer),
header: config.headers,
data: config.data,
dataType: config.dataType,
responseType: config.responseType,
sslVerify: config.sslVerify,
complete: function complete(response) {
response = {
data: response.data,
status: response.statusCode,
errMsg: response.errMsg,
header: response.header,
config: config
};
settle(resolve, reject, response);
}
})
})
}
export default adapter
3.新建index.js文件
import axios from "axios";
import adapter from "./adapter"
// 存储请求中的接口
const pending = [];
const instance = axios.create({
timeout: 60000,
headers: {
"Content-Type": "application/json",
},
});
// 封装的是适配器
instance.defaults.adapter = adapter;
/*
* 增加请求拦截器
*/
instance.interceptors.request.use(
config => {
},
(error) => {
/
}
);
/*
* 增加响应拦截器
*/
instance.interceptors.response.use(
(response) => {
},
(error) => {
}
);
export default instance;
export const request = url => {
return {
};
};
解决问题请双击66666