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,

      })

    );

axiso 高级封装的更多相关文章

  1. 【ADO.NET】7、SQL高级封装

    这次是更加简化的进行封装,所有的cmd操作命令都封装到了 Allcmd() 方法里面别外还有一个别点是 每次执行命令完后,都会垃圾回收, cmd.Parameters.Clear();是先将执行返回的 ...

  2. 微信小程序Http高级封装 es6 promise

    公司突然要开放微信小程序,持续蒙蔽的我还不知道小程序是个什么玩意. 于是上网查了一下,就开始着手开发..... 首先开发客户端的东西,都有个共同点,那就是  数据请求! 看了下小程序的请求方式大概和a ...

  3. Tensorflow高级封装

    Tensorflow比较灵活,但是它提供的操作比较低级,于是许多封装库应运而生. slim 导入方式 import tensorflow as tf import tensorflow.contrib ...

  4. JS高级——封装注册事件

    兼容性问题 1.ele.on事件类型 = function(){}一个元素ele注册一种事件多次,会被替换成最后一个,所以有局限性 2.addEventListener(事件类型,事件处理函数,use ...

  5. axios 高级封装

    import axios from 'axios'; import qs from 'qs'; const Unit = { async getApi(ajaxCfg){ let data = awa ...

  6. OC——封装(初级与高级)

    所谓的封装,就是通过定义方法或者函数去操作成员属性或者成员变量,而不是直接通过指针方式去操作.借此达到提高代码安全性,代码可行性以及代码执行效率的目的. 1:初级封装,对成员变量进行封装. #impo ...

  7. 第十六节,使用函数封装库tf.contrib.layers

    这一节,介绍TensorFlow中的一个封装好的高级库,里面有前面讲过的很多函数的高级封装,使用这个高级库来开发程序将会提高效率. 我们改写第十三节的程序,卷积函数我们使用tf.contrib.lay ...

  8. Node.js:理解stream

    Stream在node.js中是一个抽象的接口,基于EventEmitter,也是一种Buffer的高级封装,用来处理流数据.流模块便是提供各种API让我们可以很简单的使用Stream. 流分为四种类 ...

  9. Hibernate 缓存机制浅析

    1. 为什么要用 Hibernate 缓存? Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数据源 ...

随机推荐

  1. SpringCloud升级之路2020.0.x版-44.避免链路信息丢失做的设计(2)

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 我们在这一节我们将继续讲解避免链路信息丢失做的设计,主要针对获取到现有 Span 之后,如 ...

  2. 【POJ3349 Snowflake Snow Snowflakes】【Hash表】

    最近在对照省选知识点自己的技能树 今天是Hash 题面 大概是给定有n个6元序列 定义两个序列相等 当两个序列各自从某一个元素开始顺时针或者逆时针旋转排列能得到两个相同的序列 求这n个6元序列中是否有 ...

  3. 监听浏览器tab切换

    监听浏览器切屏 为了完成验证用户在切换浏览器tab时进行登录再次认证需求需要监听浏览器切换窗口 if (document.hidden !== undefined) { document.addEve ...

  4. PPT插件——iSlide全功能解析

    做幻灯展示是我们日常工作中不可缺少的一部分,有的人喜欢用代码如Latex, markdown+pandoc+revealjs 或 bookdown.这些都是自动化做幻灯的好工具.我也都有过体会,确实简 ...

  5. python(3)跳过第一行(多行)读入数据

    查了下资料,常见两种办法,一是设置行号,再者是利用python自带的itertools工具. 这里推荐一种新的方法,直接使用readline()函数就搞定. 示例: 创建一个文本文件,内容如下: 1 ...

  6. SpringBoot整合Shiro 四:认证+授权

    搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...

  7. Ubuntu Linux安装QT5之旅

    1. QT 版本选择 如何选择QT版本,参考如下介绍 https://www.cnblogs.com/chinasoft/p/15226293.html 2.  在此以5.15.0解说 下载QT 版本 ...

  8. CentOS7 搭建maven私服Nexus

    下载解压 官网https://www.sonatype.com/download-oss-sonatype 下载页面 https://help.sonatype.com/repomanager2/do ...

  9. python下载openpyxl

    直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...

  10. spring定时任务执行两次

    最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...