Uncaught TypeError: Cannot read properties of null (reading ‘getAttribute‘)

简述:vue项目报错,

Uncaught TypeError: Cannot read properties of null (reading 'getAttribute')

翻译:无法读取null属性(读取'getAttribute'),

解析:getAttribute()是js获取属性方法,就是getAttribute不能读取null的属性,

这种错误一般出现在使用Echarts和地图后,当切换到空白页面后,重置页面时,就会报类似错误,解决方法就是在报错之前做判断处理;


解决:在触发事件前做判断,假如你是在重置页面大小时触发该事件;

//给页面添加监听事件,如果存在,那再执行;
mounted() {
  this.defineWeek();
  window.addEventListener("resize", () => {
    if (document.getElementById("week")) {
      this.defineWeek();//Echarts方法
    }
  });
},

猜你喜欢

转载自blog.csdn.net/weixin_65793170/article/details/129682816