uni.createSelectorQuery()在app端返回null?

1、TypeError: Cannot read property 'left' of null

小程序和H5運行正常,但是App運行就報以下錯誤

19:10:31.860 [Vue warn]: Error in event handler for "view.onRequestComponentInfo": "TypeError: Cannot read property 'left' of null"
19:10:31.908 (found <Root>)
19:10:31.931 TypeError: Cannot read property 'left' of null

錯誤比較明顯,但是會發(fā)現(xiàn),left 這個屬性是有的,于是各種定位問題,最終發(fā)現(xiàn)問下出現(xiàn)在uni-app的Api調(diào)用時機(jī)上

又是官方文檔的鍋,官方文檔說:createSelectorQuery 方法,需要在 mounted() 生命周期調(diào)用

實際也是這么操作的,但是實際會發(fā)現(xiàn)boundingClientRect((res)) 方法中的res會一直返回null,導(dǎo)致或許全都出錯

基于之前客戶端的開發(fā)經(jīng)驗,這里肯定是取值時,還沒完全渲染完,于是,加點延遲,問題解決

# 出錯代碼
mounted() {
            setTimeout(() => {
                const query = uni.createSelectorQuery().in(this);
                query.select('.tab-box').boundingClientRect((res) => {
                    this.box = res;
                    this.updateTabWidth();
                }).exec();
            }, 0);
}

# 正確代碼
mounted() {
            setTimeout(() => {
                const query = uni.createSelectorQuery().in(this);
                query.select('.tab-box').boundingClientRect((res) => {
                    this.box = res;
                    this.updateTabWidth();
                }).exec();
            }, 300);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容