美文网首页让前端飞Web前端之路
微信小程序遇到的坑(1)回调函数 拿不到this.data里面的

微信小程序遇到的坑(1)回调函数 拿不到this.data里面的

作者: 一只大橘 | 来源:发表于2020-03-26 11:30 被阅读0次
这个坑,开发过小程序的 应该都遇到过这个问题,今天恰好有空就总结出来
事情是这样的,请看下面的内容。希望能帮到大家。

在这里设置了 resData的值


image.png

然后在回调函数里面,这个方法里面通过this.data.resData 来获取,发现是拿不到的


image.png

**Promise.resolve(value)**方法返回一个以给定值解析后的Promise 对象。如果这个值是一个 promise ,那么将返回这个 promise ;如果这个值是thenable(即带有"then"方法),返回的promise会“跟随”这个thenable的对象,采用它的最终状态;否则返回的promise将以此值完成。此函数将类promise对象的多层嵌套展平。

所以这里我是这么写的,完美解决这个问题!
这个方法是我要调用的方法,从后台接口获取的数据的方法

getOrderByStudentId: function (mac) {
    var that = this
    return new Promise(function (resolve, reject) {
      https.getOrderByStudentId(
        {
          mac: mac,
          studentId: that.data.studentId
        })
        .then(res => {
        //hasOwnProperty 是用来判断该对象里面有没有data属性 ,有就返回true.
          resolve(res.hasOwnProperty('data'))
        })
    })
  },

那么在我们的回调方法里面是如何调用的呢,请看下面代码片段

  that.getOrderByStudentId(mac.result).then(resData => {
                console.log(resData)//这个就是从上个接口返回的数据
})

总结:学海无涯,代码中的坑是要慢慢填的

相关文章

网友评论

    本文标题:微信小程序遇到的坑(1)回调函数 拿不到this.data里面的

    本文链接:https://www.haomeiwen.com/subject/rriquhtx.html