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);
}