App.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <template>
  2. <div id="app">
  3. <RouterView />
  4. <GlobalProgress />
  5. </div>
  6. </template>
  7. <script>
  8. import { getConfig } from '@/utils/auth';
  9. import GlobalProgress from '@/components/GlobalProgress.vue';
  10. export default {
  11. name: 'App',
  12. components: {
  13. GlobalProgress,
  14. },
  15. created() {
  16. // 捕获未处理的错误
  17. window.onerror = (msg, url, lineNo, columnNo, error) => {
  18. console.error('onerror', msg, url, lineNo, columnNo, error);
  19. return true;
  20. };
  21. // 捕获未处理的 Promise 拒绝错误
  22. window.addEventListener('unhandledrejection', (event) => {
  23. // 阻止 Promise 拒绝默认行为
  24. event.preventDefault();
  25. // 获取拒绝的 Promise
  26. const promise = event.promise;
  27. promise.catch((e) => {
  28. console.error('catch', e);
  29. });
  30. // 获取拒绝的原因(错误)
  31. // const reason = event.reason;
  32. // 处理 Promise 拒绝错误
  33. // console.error('未捕获的 Promise.reject 错误:', reason);
  34. });
  35. this.setTitleIcon();
  36. },
  37. methods: {
  38. setTitleIcon() {
  39. const config = getConfig();
  40. if (config) {
  41. const link = document.querySelector("link[rel*='icon']") || document.createElement('link');
  42. link.type = 'image/x-icon';
  43. link.rel = 'shortcut icon';
  44. link.href = config.title_icon_url;
  45. document.getElementsByTagName('head')[0].appendChild(link);
  46. }
  47. },
  48. },
  49. };
  50. </script>