vue 2. x中axios如何封装get和post方法

  介绍

这篇文章主要介绍vue 2。x中axios如何封装get和post方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

vue 2。x axios封装的get和post方法

import  axios 得到& # 39;axios& # 39;   import  qs 得到& # 39;qs # 39;   export  class  HttpService  {   有才能(url,数据),{   ,,,return  new 承诺((解决,,拒绝),=祝辞,{   ,,,,,axios.get (url,, {   ,,,,,,,参数:数据   ,,,,,})((res),=祝辞,{   ,,,,,,,if  (res), {   ,,,,,,,,,//成功回调   ,,,,,,,,,解决(res);   ,,,,,,,}   ,,,,,}).catch((错误),=祝辞,{   ,,,,,,,拒绝(错误);   ,,,,,})   ,,,})   ,,}   后才能(url,数据),{   ,,,return  new 承诺((解决,,拒绝),=祝辞,{   ,,,,,axios.post (url, qs.stringify(数据),{   ,,,,,,,头:,{   ,,,,,,,,,& # 39;内容类型# 39;:,& # 39;应用程序/x-www-form-urlencoded& # 39;   ,,,,,,,,,& # 39;接受# 39;:,& # 39;application/json # 39;   ,,,,,,,}   ,,,,,})((res),=祝辞,{   ,,,,,,,if  (res), {   ,,,,,,,,,//成功回调   ,,,,,,,,,解决(res);   ,,,,,,,}   ,,,,,}).catch((错误),=祝辞,{   ,,,,,,,拒绝(错误);   ,,,,,})   ,,,})   ,,}   }

<>强,postfile方法

PostFlie (url,数据),{   ,,,return  new 承诺((解决,,拒绝),=祝辞,{   ,,,,,//根据数据对象生成FormData对象   ,,,,,var  temp =, new  FormData ();   ,,,,,for  (var  t 数据拷贝),{   ,,,,,,,temp.append (t),数据[t]);   ,,,,,}   ,,,,,axios.post (url,临时),然后((res),=祝辞,{   ,,,,,,,if  (res), {   ,,,,,,,,,,,解决(res.Data);   ,,,,,,,}   ,,,,,}).catch((错误),=祝辞,{   ,,,,,,,拒绝(错误);   ,,,,,})   ,,,})   以前,,}

以上是“vue 2. x中axios如何封装get和post方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

vue 2. x中axios如何封装get和post方法