使用uni.createSelectorQuery注意事项

  • Post author:
  • Post category:其他


一、因为作用是获取节点上的信息,所以必须在mounted 后使用,下面是使用方法之一,或者在需要的时候获取一样的

 mounted() {
        // 获取节点信息
        this.$nextTick(() => {
            this.getSelectInfo();
        });
    }

 getSelectInfo() {
    const query = uni.createSelectorQuery().in(this);
   
    query.select('#id  / .className').boundingClientRect(data => {
      
      console.log("节点离页面顶部的距离为" + data.top);
    }).exec();

    }

二、作者使用此API场景是开发APP(下面是实际代码),避坑点。


1、代码的执行的顺序:


先执行条件外的,再执行条件里面的


2、节点返回的数据





datas:{



“height”:18,


“dataset”:{“v-241be720″:””},


“right”:383.0909118652344,


“id”:””,


“bottom”:45.6363639831543,


“left”:10,


“width”:373.0909118652344,


“top”:27.636363983154297}

   getSelectInfo() {
            // #ifdef H5
            const doc = document.getElementsByClassName('select');
            this.offWidth = ((doc[0].offsetWidth+20) / 8 / 60).toFixed(2); //每分钟的距离 px
            this.countOffWidth();
            // #endif

            // #ifdef APP-PLUS
            const query = uni.createSelectorQuery().in(this);
            query
                .select('.select')
                .boundingClientRect((data) => {

                    let datas = JSON.stringify(data);
                    console.log('Gongdb--->datas',datas);
                    this.currentTime();
                    
                    this.offWidth = ((data.width-10) / 8 / 60).toFixed(2); //每分钟的距离 px
                    this.countOffWidth();
                })
                .exec();
            // #endif
            this.currentTime();
        },



版权声明:本文为m0_67640304原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。