微信小程序实现接口Promise化

微信小程序实现接口Promise化

  • 安装依赖
npm i miniprogram-api-promise@1.0.4 --save
  • 构建小程序
在微信开发工具中->工具->构建npm
  • 在小程序的入口文件app.js
// 使用miniprogram-api-promise实现接口promise化

// 在小程序入口文件中(app.js)只需要调用一次 promisifyAll() 方法实现 API的promise化。
import {promisifyAll} from "miniprogram-api-promise";
// 定义常量wxp 和 wx.p 属性
const wxp = wx.p = {}
// 将 wxp的空对象绑定在 promisifyAll上
promisifyAll(wx, wxp);

App({})

  • 使用 xxx.js 中使用
async getInfo(){
  const res = await  wx.p.request({
    url: "your custom api",
    method: "GET",
    data:{}
  })
  console.log(res);
}
posted @ 2022-07-14 22:42  巫小诗  阅读(550)  评论(0编辑  收藏  举报