美文网首页
简单封装axios请求

简单封装axios请求

作者: 你若像风 | 来源:发表于2019-05-07 10:51 被阅读0次
    import axios from "axios";
    import { MessageBox } from "element-ui";
    
    function handleError(message, reject) {
      if (message) {
        MessageBox.alert(message, "提示");
      } else {
        MessageBox.alert("网络或系统错误!", "提示");
      }
      reject();
    }
    
    export default {
      get(path, data = {}) {
        let p = new Promise(async function(resolve, reject) {
          try {
            let resp = await axios.get(path, data);
            if (resp.data.errcode == 0) {
              resolve(resp.data);
            } else {
              handleError(resp.data.errmsg, reject);
            }
          } catch (e) {
            handleError(e, reject);
          }
        });
        return p;
      },
    
      post(path, data = {}) {
        let p = new Promise(async function(resolve, reject) {
          try {
            let resp = await axios.post(path, data);
            if (resp.data.errcode == 0) {
              resolve(resp.data);
            } else {
              handleError(resp.data.errmsg, reject);
            }
          } catch (e) {
            handleError(e, reject);
          }
        });
        return p;
      }
    };
    
    

    相关文章

      网友评论

          本文标题:简单封装axios请求

          本文链接:https://www.haomeiwen.com/subject/vizsoqtx.html