loading.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. if (binding.oldValue !== binding.value) {
  27. instance.setLoading?.(binding.value && !instance.loading);
  28. }
  29. }
  30. },
  31. unmounted(el) {
  32. el?.instance?.close();
  33. },
  34. };
  35. export function setupLoadingDirective(app: App) {
  36. app.directive('loading', loadingDirective);
  37. }
  38. export default loadingDirective;