axios请求方式封装

/**
 * GET请求封装
 *
 * */
Vue.prototype.$get = function (url, data, fun, err) { //url接口 data 传入数据 fun 成功方法 err 错误方法
      axios({
        method: 'get',
        url: url,
        headers: {
            'token': localStorage.getItem("token"),
        },
          params: data,
    }).then(function (res) {
        if (fun) {
            fun(res)
        }
    }).catch(function (error) {
        if (err) {
            err(error)
        }
    });
}

/**
 * POST请求封装
 *
 * */
Vue.prototype.$post = function (url, data, fun, err) {
      axios({
        method: 'post',
        url: url,
        headers: {
            'token': localStorage.getItem("token"),
        },
          data: data
    }).then(function (res) {
        if (fun) {
            fun(res)
        }
    }).catch(function (error) {
        if (err) {
            err(error)
        }
    });
}
原文地址:https://www.cnblogs.com/wtwebskill/p/13953416.html