wx.createSelectorQuery()如何返回数据

发布于 7 年前作者 motao11389 次浏览最后编辑 7 年前来自 ask
  • 需求的场景描述(希望解决的问题)

let that = this

console.log(wx.createSelectorQuery().select(‘#id’).boundingClientRect(function(rect){

      //  可以在此处赋值,不知如何将获取到的值返回?

      that.setData({

        key: rect.height

      }) 

    }).exec())

//  输出: undefined

  • 希望提供的能力

let that = this

let height = 0

wx.createSelectorQuery().select(‘#id’).boundingClientRect(function(rect){

      //  可以在此处赋值,不知如何将获取到的值返回?

      that.setData({

        key: rect.height

      })

      height = rect.height

}).exec()

return height

例如将获取参数的行为,封装成一个方法,取到值后返回相应的值。该操作应该是异步的,所以在调用Api之后返回数据,是0.

不知道是否有相关的取值方式,自己试了在exec()中返回也是无效。

2 回复
xuefang
xuefang1 楼6 年前

我也碰到了,要怎么解决

yanglong
yanglong2 楼5 年前

用settimeout延迟执行其他函数, 这个API函数无法返回值