wx.createSelectorQuery()如何返回数据
发布于 6 年前 作者 motao 11249 次浏览 来自 问答
  • 需求的场景描述(希望解决的问题)

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 回复

我也碰到了,要怎么解决

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

回到顶部