wepy使用Promise简易封装网络请求

36次阅读

共计 433 个字符,预计需要花费 2 分钟才能阅读完成。

network/request.js

// 封装 ajax 请求

const http = (url,type,parameter) => {return new Promise((resolve,reject) => {
    wx.request({
      url:url,
      method:type,
      success:function (res) {resolve(res.data);
      },
      fail:function (err) {reject(err)
      }
    })
  })

}

export {http}

在 index.wpy 中使用

    import {http} from '../network/request';
    http("https://easy-mock.com/mock/5cc66aee7a9a541c744c9c07/example/restful/:id/list","GET").then(function (res) {console.log(res)
    }).catch(function (err) {});

我的微信公众号:天字一等

正文完
 0