收藏
回答

wx.createSelectorQuery里面boundingClientRect方法获取的问题?

  getSelector(arr = [], name = 'b-image') {

    let $this = this

    let {

      dataList

    } = this.data



    return new Promise((resolve, reject) => {

      let arr2 = JSON.parse(JSON.stringify(arr))

      setTimeout(() => {

        let i = -1;

        arr2.forEach((el, index) => {

          wx.createSelectorQuery().select(`#${name+index}`).boundingClientRect(res => {

            el.pageHeight = res.height

            // console.log(res.height);

            i += 1

            if (index == i{

              resolve(arr2)

            }

          }).exec()

        })

      }, 500);

    })

  },


我循环了一个数组并且在每个元素上绑定了一个id ,然后 我同promise返回结果出去,然后我在Promise回调里面拿到那些值做处理,为什么有时候返回的是undefined?然后再手机上返回的都是正常的,在编译器里面就有问题,这是啥原因

$this.getSelector(res.data.Data).then(res1 => {

            console.log(res1);

            res1.forEach(el => {

              console.log(el.pageHeight);

              if (leftHeight == rightHeight{


                leftHeight += !Number(el.pageHeight? 20 : Number(el.pageHeight)

                // console.log(leftHeight, 'leftHeight');

                // console.log(el.pageHeight,'Number(el.pageHeight)');

                leftList.push(el)


              } else if (leftHeight > rightHeight{

                rightList.push(el)

                rightHeight += !Number(el.pageHeight? 20 : Number(el.pageHeight)

                // console.log(el.pageHeight,'Number(el.pageHeight)');

                // console.log(rightHeight, 'rightHeight');

              } else {

                leftList.push(el)

                leftHeight += !Number(el.pageHeight? 20 : Number(el.pageHeight)

                // console.log(el.pageHeight,'Number(el.pageHeight)');

                // console.log(leftHeight, 'leftHeight');

              }

            })

            $this.setData({

              leftList,

              rightList,

              leftHeight,

              rightHeight

            })

          })



        })

      }

    })

  }


回答关注问题邀请回答
收藏
登录 后发表内容