绑定滚动事件,获取元素距离屏幕高度,经常出现报错,无此functions
<div ref="showMore"></div>
mounted:function () {
window.addEventListener('scroll', this.handleScroll);
}
handleScroll(){
this.$nextTick(()=>{
console.log(this.$refs.showMore.getBoundingClientRect())
})
}
加不加this.$nextTick都会出现错误。。有时候滚动如1所示没错,有时候滚动的时候就如2所示,报错: Error in nextTick: "TypeError: Cannot read property 'getBoundingClientRect' of undefined"
console.log(this.$refs)的结果:
使用
Vant
的van-tabs
在$router.push
时出现了,复现流程比较复杂。其中
TypeError: Cannot read property 'getBoundingClientRect' of undefined
经过定位是在Vant
的getVisibleHeight
方法中没有获取到el的原因。大概率是
Vant
的问题。倒不是bug,缺少了些边界值判断吧。最后是通过添加一个setTimeout(() => { /* something */ }, 0)
解决了问题。