Explorar el Código

feat:dealWithMessage

ananzhusen hace 1 año
padre
commit
11d918ae75
Se han modificado 2 ficheros con 14 adiciones y 9 borrados
  1. 7 5
      src/views/Preview.vue
  2. 7 4
      src/views/components/Header.vue

+ 7 - 5
src/views/Preview.vue

@@ -36,8 +36,11 @@ onMounted(() => {
   registerBasicDiagram();
   open();
   meta2d.on('opened', opened);
-  window.addEventListener('message', function (e) {
-    if (typeof e.data !== 'string') {
+  window.addEventListener('message', dealWithMessage);
+});
+
+const dealWithMessage = (e)=>{
+  if (typeof e.data !== 'string') {
       return;
     }
     try {
@@ -55,8 +58,7 @@ onMounted(() => {
     } catch (e) {
       console.info(e);
     }
-  });
-});
+}
 
 const watcher = watch(
   () => route.query.id,
@@ -111,9 +113,9 @@ onUnmounted(() => {
   watcher();
   if (meta2d) {
     meta2d.off('opened', opened);
-
     meta2d.destroy();
   }
+  window.removeEventListener('message', dealWithMessage);
 });
 </script>
 <style lang="postcss" scoped>

+ 7 - 4
src/views/components/Header.vue

@@ -386,8 +386,11 @@ let iframeNum = 0;
 let compareNum = 0;
 nextTick(() => {
   meta2d.on('opened', initMeta2dName);
-  window.addEventListener('message', function (e) {
-    if (typeof e.data !== 'string'||!e.data||e.data.startsWith('setImmediate')) {
+  window.addEventListener('message', dealWithMessage);
+});
+
+const dealWithMessage = (e)=>{
+  if (typeof e.data !== 'string'||!e.data||e.data.startsWith('setImmediate')) {
       return;
     }
     try {
@@ -407,11 +410,11 @@ nextTick(() => {
     } catch (e) {
       console.info(e);
     }
-  });
-});
+}
 
 onUnmounted(() => {
   meta2d.off('opened', initMeta2dName);
+  window.removeEventListener('message', dealWithMessage);
 });
 
 const login = () => {