vue2 接口数据量太大导致页面崩溃

vue yekong 52℃

vue2 数据可视化大屏项目开发中,需要渲染geoJson数据到地图上,但是geoJson数据量太大,有几十兆的大小,并且多个页面会访问此接口,当页面切换后数据量太大积累的数据并没有从内存中清除导致多次切换路由后内存从几十M内存占用到达1000多M,进而导致内存小的电脑网页崩溃。

vue2 接口数据量太大导致页面崩溃

解决思路1 v-if

一开始是用v-if来处理,当路由页面跳转离开当前路由时,通过设置false来销毁组件,从而达到清除内存缓存,实际效果却不大,切换几次路由后,内存依然会大幅度增加。

 <tMap v-if="showTMap" :zoom="7.2"></tMap>
beforeRouteLeave(to, from, next) {
// 在路由跳转前设置showTMap为false来销毁tMap组件
this.showTMap = false;
next();
}

解决思路2

第二个思路是设置一个变量接收接口返回的数据

var response = null

当数据使用完毕后,将response设置为null

response = await axios.post(url, data, {
  headers: {
    'Content-Type': 'application/xml', // 根据服务器要求设置适当的Content-Type
  },
});
// 处理响应数据
this.addGeoJsonToMap(response.data);
// 渲染完成后,清除原始GeoJSON数据
response = null;
riverCodes = null;
filterConditions = null;

然后在组件销毁前,把所有涉及到大内存的变量都置空

  // 在组件卸载或路由跳转前调用
  beforeDestroy() {
    // 清除原始GeoJSON数据
    response = null;
    riverCodes = null;
    filterConditions = null;
    vectorLayer = null;
    vectorSource = null;
    if (this.cancel) {
      this.cancel.cancel('组件卸载,取消请求');
    }
    if (this.map) {
      this.map.setTarget(null); // 解除地图绑定
      this.map = null; // 清除地图实例引用
    }
    if (this.riverLayer) {
      this.riverLayer.getSource().clear(); // 清除图层数据
      this.map.removeLayer(this.riverLayer); // 从地图中移除图层
      this.riverLayer = null; // 清除图层引用
    }
    riverList = null;
    this.riverList = null;
  }

通过第二种方法内存增长量减少了很多,有一定的效果。

喜欢 (0)