loading.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { createLoading } from '@/components/Loading';
  2. import type { Directive, App } from 'vue';
  3. const loadingDirective: Directive = {
  4. mounted(el, binding) {
  5. const tip = el.getAttribute('loading-tip');
  6. const background = el.getAttribute('loading-background');
  7. const size = el.getAttribute('loading-size');
  8. const fullscreen = !!binding.modifiers.fullscreen;
  9. const instance = createLoading(
  10. {
  11. tip,
  12. background,
  13. size: size || 'large',
  14. loading: !!binding.value,
  15. absolute: !fullscreen,
  16. },
  17. fullscreen ? document.body : el,
  18. );
  19. el.instance = instance;
  20. },
  21. updated(el, binding) {
  22. const instance = el.instance;
  23. if (!instance) return;
  24. instance.setTip(el.getAttribute('loading-tip'));
  25. if (binding.oldValue !== binding.value) {
  26. instance.setLoading?.(binding.value && !instance.loading);
  27. }
  28. },
  29. unmounted(el) {
  30. el?.instance?.close();
  31. },
  32. };
  33. export function setupLoadingDirective(app: App) {
  34. app.directive('loading', loadingDirective);
  35. }
  36. export default loadingDirective;