axios的二次封装
把此代码放进js文件里 使用的时候引入调用内方法即可!
import axios from 'axios'; const Unit = { async getApi(ajaxCfg){ let data = await axios.get(ajaxCfg.url,{params:ajaxCfg.cfg}, { headers: ajaxCfg.headers }) return data; }, async getApi2(url,cfg,headers){ let data = await axios.get(url,{params:cfg}, { headers: headers }) return data; }, async postApi(url,cfg,headers){ let fd = new FormData(); for(let key in cfg){ fd.append(key, cfg[key]); } let data = await axios.post(url,fd, { headers: headers }) return data; }, async putApi(url,cfg,headers){ // import qs from 'qs'; // let data = await axios.put(url,qs.stringify(cfg),{ // headers: { // 'Content-Type':'application/x-www-form-urlencoded', // } // }) // return data; }, async requestApi(cfg,headers,file){ let fd = new FormData(); fd.append('param', JSON.stringify(cfg)); if(file){ // 上传证明 if(file.length){ for(let i=0,len=file.length;i<len;i++){ fd.append('files', file[i]); } }else { // 单个上传 for(let key in file){ fd.append(key, file[key]); } } } let data = await axios.post('/batch',fd, { headers: headers }) return data; } } export default Unit;
CSDN本章网址:https://blog.csdn.net/lastone1212/article/details/117332524