axios 高级封装
import axios from 'axios';
import qs from 'qs';
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,cfg,
{
headers: headers
})
return data;
},
async putApi(url,cfg,headers){
let data = await axios.put(url,qs.stringify(cfg),{
headers: {
'Content-Type':'application/x-www-form-urlencoded',
}
})
return data;
},
async postApi2(url,cfg,headers){
let data = await axios.post(url,cfg,
{
headers: headers
})
return data;
},
async postApi3(url,cfg,headers){
let data = await axios.post(url,qs.stringify(cfg),{
headers: {
'Content-Type':'application/x-www-form-urlencoded',
}
})
return data;
},
async delApi(url,cfg,headers){
let data = await axios.delete(url,{params:cfg},{
headers: headers
})
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;
// get all
Unit.getApi2('/users',{},{}).then((res)=>{
console.log(res.data)
})
// // // get one
const id = 33;
Unit.getApi2(`/users/${id}`,{},{}).then((res)=>{
console.log(res.data)
})
//更新
Unit.putApi(`/users/${id}`,{name:1,age:32},{}).then((res)=>{
console.log(res.data)
})
//删除ok
// let _id = 33;
// Unit.delApi(`/users/${_id}`,{},{}).then((res)=>{
// console.log(res.data)
// })
// 插入数据
Unit.postApi2('/users',{name:4,age:32},{}).then((res)=>{
console.log(res.data)
}) app.use(
'/users',createProxyMiddleware({
target: 'http://127.0.0.1:7001/',
changeOrigin: true,
})
);
本人CSDN本章地址:https://blog.csdn.net/lastone1212/article/details/117366246
axios 高级封装的更多相关文章
- vue中Axios的封装和API接口的管理
前端小白的声明: 这篇文章为转载:主要是为了方便自己查阅学习.如果对原博主造成侵犯,我会立即删除. 转载地址:点击查看 如图,面对一团糟代码的你~~~真的想说,What F~U~C~K!!! 回归正题 ...
- vue中对axios进行封装
在刚结束的项目中对axios进行了实践(好不容易碰上一个不是jsonp的项目), 以下为在项目中对axios的封装,仅封装了post方法,因为项目中只用到了post,如有需要请自行进行修改 src/c ...
- axios方法封装
axios方法封装 一般情况下,我们会用到的方法有:GET,POST,PUT,PATCH,封装方法如下: 五.封装后的方法的使用 1.在main.js文件里引用之前写好的文件,我的命名为htt ...
- 聊聊 Vue 中 axios 的封装
聊聊 Vue 中 axios 的封装 axios 是 Vue 官方推荐的一个 HTTP 库,用 axios 官方简介来介绍它,就是: Axios 是一个基于 promise 的 HTTP 库,可以用在 ...
- Vue中axios的封装和api接口的统一管理
更新的是我csdn上的文章,需要的话可以看下,互相学习点击去我的csdn vue中axios的封装 在vue项目和后端交互获取数据时,通常使用axios库,官方文档:https://www.npmjs ...
- 三、VUE项目BaseCms系列文章:axios 的封装
项目开发中 ajax 是不可缺少的,一个好的封装可以减少我们很多的重复代码,维护也更方便.在 vue 开发中我们用的比较多的就是 axios.下面代码是项目中用到的 axios 的封装. http.j ...
- vue中axios的封装以及简单使用
一.axios的封装 在vue中为了使用axios使用方便,不需要每一个模块进行导入,就需要对其进行封装: 1.新建http.js模块 import axios from 'axios' // 设置基 ...
- vue axios接口封装、Promise封装、简单的axios方法封装、vue接口方法封装、vue post、get、patch、put方法封装
相信大家在做前后端数据交互的时候都会给请求做一些简单的封装就像之前封装ajax方法一样axios的封装也是一样的简单下面这个就是封装的axios的方法,require.js import axios ...
- 第五十五篇:Axios的封装
好家伙, 上图 1.为什么需要封装axios? 当我们改变项目的使用环境时候,url也会随之改变,那么我们就需要改很多axios请求中的url配置 现在我们将axios封装,在项目使用环境改变时我们只 ...
随机推荐
- URL URI傻傻分不清楚,dart告诉你该怎么用
目录 简介 dart中的URI encode和decode 解析URI 总结 简介 如果我们要访问一个网站,需要知道这个网站的地址,网站的地址一般被称为URL,他的全称是Uniform Resourc ...
- Codeforces 446C - DZY Loves Fibonacci Numbers(斐波那契数列+线段树)
Codeforces 题目传送门 & 洛谷题目传送门 你可能会疑惑我为什么要写 *2400 的题的题解 首先一个很明显的想法是,看到斐波那契数列和 \(10^9+9\) 就想到通项公式,\(F ...
- Redis总结笔记
Redis总结笔记 应用场景 缓存--热数据 计算器 队列 位操作 分布式锁与单线程机制 最新列表 排行榜 Maxmemory-policy算法 volatile-lru:使用LRU算法移除key ...
- mysql—MySQL数据库中10位或13位时间戳和标准时间相互转换
1.字符串时间转10位时间戳 select FLOOR(unix_timestamp(create_time)) from page; #create_time为字段名 page为表名 eg:sele ...
- 9. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- 修复UE4编辑器,ClearLog操作导致的崩溃
UE4 4.24.3版本,编辑器Output Log窗口中,右键--Clear Log操作很大概率会导致编辑器奔溃:解决办法: 相关文件: Engine\Source\Developer\Output ...
- 剖析ApplicationRunner、CommandLineRunner
需求:SpringBoot项目启动成功后执行某方法 方案:在spring-boot中提供了两种Runner接口:ApplicationRunner和CommandLineRunner,编写自己的类实现 ...
- A Child's History of England.48
A few could not resolve to do this, but the greater part complied. They made a blazing heap of all t ...
- A Child's History of England.31
The English in general were on King Henry's side, though many of the Normans were on Robert's. But t ...
- Hive(三)【DDL 数据定义】
目录 一.DDL数据定义 1.库的DDL 1.1创建数据库 1.2查询数据库 1.3查看数据库详情 1.4切换数据库 1.5修改数据库 1.6删除数据库 2.表的DDL 2.1创建表 2.2管理表(内 ...