uni.createSelectorQuery渲染後執行

每次使用createSelectorQuery時,因爲和數據賦值一起調用,所以導致只能獲取到上一次渲染的數據,

如果能讓這個函數 在渲染之後調用,就可以完美解決了

暫時不知道怎麼監聽頁面是否渲染。

所以只能使用 setTimeout臨時代替一下

	scrollToBottom: function () {
					let query = uni.createSelectorQuery();
					query.in(this).selectAll('.main-item').boundingClientRect();
					query.in(this).select('#scrollview').boundingClientRect();
					query.exec((res) => {
							let mitemHeight = 0;
							console.log("元素",res);
							res[0].forEach((rect) => mitemHeight = mitemHeight + rect.height + 40)  
							if (mitemHeight > that.solHeight) {  
									that.scrollTop = (mitemHeight + 100)
							}
	    })
			}

注意一下 

官方文檔

  • 使用 uni.createSelectorQuery() 需要在生命週期 mounted 後進行調用。
  • 自定義組件編譯模式(默認模式),需要使用到 selectorQuery.in 方法。

 調用時 使用

setTimeout(() => {
				       that.scrollToBottom();
				}, 100)

就可以暫時解決這個問題

找到了更優解

that.$nextTick(function(){
					  that.scrollToBottom();
				})

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章