main.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { preferencesManager } from '@vben-core/preferences';
  2. import { overridesPreferences } from './preferences';
  3. /**
  4. * 应用初始化完成之后再进行页面加载渲染
  5. */
  6. async function initApplication() {
  7. // name用于指定项目唯一标识
  8. // 用于区分不同项目的偏好设置以及存储数据的key前缀以及其他一些需要隔离的数据
  9. const env = import.meta.env.PROD ? 'prod' : 'dev';
  10. const namespace = `${import.meta.env.VITE_APP_NAMESPACE}-${env}`;
  11. // app偏好设置初始化
  12. await preferencesManager.initPreferences({
  13. namespace,
  14. overrides: overridesPreferences,
  15. });
  16. // 启动应用并挂载
  17. // vue应用主要逻辑及视图
  18. const { bootstrap } = await import('./bootstrap');
  19. await bootstrap(namespace);
  20. // 移除并销毁loading
  21. destroyAppLoading();
  22. }
  23. /**
  24. * 移除并销毁loading
  25. * 放在这里是而不是放在 index.html 的app标签内,是因为这样比较不会生硬,渲染过快可能会有闪烁
  26. * 通过先添加css动画隐藏,在动画结束后在移除loading节点来改善体验
  27. * 不好的地方是会增加一些代码量
  28. */
  29. function destroyAppLoading() {
  30. // 查找全局 loading 元素
  31. const loadingElement = document.querySelector('#__app-loading__');
  32. if (loadingElement) {
  33. // 添加隐藏类,触发过渡动画
  34. loadingElement.classList.add('hidden');
  35. // 查找所有需要移除的注入 loading 元素
  36. const injectLoadingElements = document.querySelectorAll(
  37. '[data-app-loading^="inject"]',
  38. );
  39. // 当过渡动画结束时,移除 loading 元素和所有注入的 loading 元素
  40. loadingElement.addEventListener(
  41. 'transitionend',
  42. () => {
  43. loadingElement.remove(); // 移除 loading 元素
  44. injectLoadingElements.forEach((el) => el.remove()); // 移除所有注入的 loading 元素
  45. },
  46. { once: true },
  47. ); // 确保事件只触发一次
  48. }
  49. }
  50. initApplication();