lex 1 년 전
부모
커밋
721e8011bc
2개의 변경된 파일40개의 추가작업 그리고 0개의 파일을 삭제
  1. 34 0
      src/hooks/useErrorLog/index.ts
  2. 6 0
      src/main.ts

+ 34 - 0
src/hooks/useErrorLog/index.ts

@@ -0,0 +1,34 @@
+import { useRoute } from 'vue-router';
+
+/**
+ * 页面有报错时上传错误日志
+ * @params
+ */
+export default function useErrorLog() {
+  const _whiteBlanks = [];
+
+  const _uploadErrorLog = async (e: any) => {
+    const route = useRoute();
+
+    console.log(route, e);
+  };
+  /**
+   * 开始监听错误日志并上传
+   */
+  const startListenErrorLog = () => {
+    // console.log('mount useErrorLog');
+    window.addEventListener('error', _uploadErrorLog);
+  };
+
+  /**
+   * 停止监听
+   */
+  const stopListenErrorLog = () => {
+    window.removeEventListener('error', _uploadErrorLog);
+  };
+
+  return {
+    startListenErrorLog,
+    stopListenErrorLog
+  };
+}

+ 6 - 0
src/main.ts

@@ -13,6 +13,7 @@ import { storage } from '@/helpers/storage';
 import { ACCESS_TOKEN } from '@/store/mutation-types';
 import { setupStore } from './store';
 import { Lazyload, setToastDefaultOptions } from 'vant';
+import useErrorLog from './hooks/useErrorLog';
 setToastDefaultOptions({ duration: 3000 });
 
 // 获取token
@@ -34,6 +35,10 @@ postMessage({ api: 'getNavHeight' }, (res: any) => {
   }
 });
 
+// 监听错误信息
+const errorLog = useErrorLog();
+errorLog.startListenErrorLog();
+
 // import Vconsole from 'vconsole';
 // const vconsole = new Vconsole();
 
@@ -47,4 +52,5 @@ setupStore(app);
 dayjs.locale('zh-ch');
 
 app.use(router);
+
 app.mount('#app');