Просмотр исходного кода

fix: 解决热更新时组件销毁出现getMap方法调用失败问题

sequoia tungfang 1 месяц назад
Родитель
Сommit
7c915b07a9
1 измененных файлов с 26 добавлено и 3 удалено
  1. 26 3
      src/components/TongzhouTrafficMap.vue

+ 26 - 3
src/components/TongzhouTrafficMap.vue

@@ -70,9 +70,32 @@ export default {
   },
   beforeDestroy() {
     if (this.infoWindow) this.infoWindow.close();
-    this.polylines.forEach(p => p.setMap(null));
-    Object.values(this.routeGroups).forEach(g => g.setMap(null));
-    if (this.map) this.map.destroy();
+
+    // 1. 清理普通的 polylines 数组
+    this.polylines.forEach(p => {
+      if (p && typeof p.setMap === 'function') p.setMap(null);
+    });
+
+    // 2. 核心修改:清理 routeGroups
+    Object.values(this.routeGroups).forEach(g => {
+      if (!g) return;
+      
+      if (Array.isArray(g)) {
+        // 如果是数组(当前的逻辑),遍历每个成员销毁
+        g.forEach(overlay => {
+          if (overlay && typeof overlay.setMap === 'function') {
+            overlay.setMap(null);
+          }
+        });
+      } else if (typeof g.setMap === 'function') {
+        // 如果是旧版的 OverlayGroup 或单个覆盖物
+        g.setMap(null);
+      }
+    });
+
+    if (this.map) {
+      this.map.destroy();
+    }
   },
   computed: {
     // 判断是否所有图例都在激活列表中