封装vue-resource http请求

import Vue from "vue"
import vueResource from "vue-resource"
Vue.use(vueResource)
export default{
  getRequest(url,cb) {
    return new Promise((resolve, reject) => {
      Vue.http.get(
        url,
        {emulateJSON: true}
      )
        .then(cb)
        .catch((err) => {
          reject(err);
        });
    });
  },
postRequest(url, params,cb) {
  return new Promise((resolve, reject) => {
    Vue.http.post(
      url,
      params,
      {emulateJSON: true}
    )
      .then(cb)
      .catch((err) => {
        reject(err);
      });
  });
}
}

import http from "http.js" 
//使用方法 var url = url; //url地址 
var params=params; 
http.postRequest(url, params,(res)=>{ console.log(res) })
  

  

posted @ 2018-08-05 21:45  斯丢皮德曼  阅读(2963)  评论(0编辑  收藏  举报