1. 程式人生 > 其它 >chunk-vendors.js:25979 Uncaught TypeError: Cannot read properties of null (reading 'bottom') at IntersectionObserver.c.<computed>.IntersectionObserver.root (chunk-vendors.js:26017:15)

chunk-vendors.js:25979 Uncaught TypeError: Cannot read properties of null (reading 'bottom') at IntersectionObserver.c.<computed>.IntersectionObserver.root (chunk-vendors.js:26017:15)

報錯截圖:

 ps:這個問題不好描述,找了好久,問題都沒能解決,最終找到原因:

報錯原因:

sticky元件建立了Observer監聽,當切換頁面且頁面沒有銷燬(例如:tabbar頁面),

sticky元件也沒有銷燬,自然beforeDestroy沒有生效,導致元件仍然保持監聽,所以出現Cannot read property 'bottom' of null報錯。

所以我們需要手動斷開監聽來解決這個報錯;

 1 <template>  
 2     <view>  
 3         <!--  @property {Boolean} enable 是否開啟吸頂功能(預設true)-->  
 4
<u-sticky :enable="enable" h5-nav-height="0"> 5 <view> 6 …… 7 </view> 8 </u-sticky> 9 </view> 10 </template> 11 <script> 12 export default { 13 data() { 14 return {
15 // 是否開啟吸頂功能 16 enable: true 17 } 18 }, 19 // 在對應的show和hide頁面生命週期中開啟或關閉監聽 20 onShow() { 21 this.enable= true 22 }, 23 onHide() { 24 //頁面銷燬時,取消監聽 25 this.enable= false 26 } 27 } 28 </script>

 問題完美解決!

如果有疑問,歡迎留言討論!