在项目中碰到的问题,uniapp使用uni.createSelectorQuery().in()获取节点,方法在onReady中调用,但是始终无法获取到循环后的节点,后来加了一个setTimeout就解决了问题
//方法需要在生命周期mounted或者onReady后进行调用
onReady: function() {
setTimeout(function(){
this.getNodesInfo();
},1000)
},
//获取节点
getNodesInfo(){
const query =uni.createSelectorQuery().in(this);
query.selectAll(".productTitle").boundingClientRect().exec(res=>{
console.log(res);
let nodes = res[0];
let arr = [];
nodes.map(item=>{
arr.push(item.top);
})
this.topList = arr;
console.log(this.topList);
})
},