z-hj

微信小程序调用外部js中的wx.request方法时,因为异步的请求机制,我们不能在其success:function()中直接返回需要的数据。

例子:

一:

//此方法处于外部文件 “utils/util.js” 中进行了定义
function request_method(url, callback)
{
  wx.request({
    url: url,
    method: \'GET\',
    header: {
      \'Content-Type\': \'application/json\'
    },
    success: function (res) {
      callback && callback(res.data);
    }
  });
}

//需要加上这段来暴露你定义的方法,否则在外部找不到
module.exports = {
request_method:request_method
}
 

二 当前页面对应 js方法的:

//首先要引入公共js
var util = require(\'../../utils/util.js\');

util.request_method(url, (res) => {
      this.setData({
        otherData: res
      });
});

 

分类:

技术点:

相关文章:

  • 2021-10-06
  • 2021-12-18
  • 2021-12-28
  • 2021-11-25
  • 2021-12-18
  • 2022-01-22
  • 2021-12-28
  • 2021-10-18
猜你喜欢
  • 2021-10-04
  • 2021-10-08
  • 2022-02-08
  • 2021-05-31
  • 2021-12-07
  • 2021-11-02
  • 2021-12-30
相关资源
相似解决方案