unmount-global-loading.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. /**
  2. * 移除并销毁loading
  3. * 放在这里是而不是放在 index.html 的app标签内,是因为这样比较不会生硬,渲染过快可能会有闪烁
  4. * 通过先添加css动画隐藏,在动画结束后在移除loading节点来改善体验
  5. * 不好的地方是会增加一些代码量
  6. */
  7. export function unmountGlobalLoading() {
  8. // 查找全局 loading 元素
  9. const loadingElement = document.querySelector('#__app-loading__');
  10. if (loadingElement) {
  11. // 添加隐藏类,触发过渡动画
  12. loadingElement.classList.add('hidden');
  13. // 查找所有需要移除的注入 loading 元素
  14. const injectLoadingElements = document.querySelectorAll(
  15. '[data-app-loading^="inject"]',
  16. );
  17. // 当过渡动画结束时,移除 loading 元素和所有注入的 loading 元素
  18. loadingElement.addEventListener(
  19. 'transitionend',
  20. () => {
  21. loadingElement.remove(); // 移除 loading 元素
  22. injectLoadingElements.forEach((el) => el.remove()); // 移除所有注入的 loading 元素
  23. },
  24. { once: true },
  25. ); // 确保事件只触发一次
  26. }
  27. }