Uncaught TypeError: Cannot read property 'offsetLeft' of undefinedat window.onscroll
正常业务场景:
mounted() { window.οnscrοll=(()=>{ let sonBox=document.getElementsByName('sonBox') ; this.yxzyLeft=sonBox[0].offsetLeft+sonBox[0].clientWidth-100+'px' ; if(window.pageYOffset>=53){ this.controllerZyZTop=true; }else{ this.controllerZyZTop=false; } // console.log(window.pageYOffset); }) },
解决方式在beforeRouteLeave钩子中写
beforeRouteLeave (to, from, next) { //离开路由前清空onscroll的所有监听内容 window.onscroll = ()=> { } next() },