index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div class="p-5" ref="wrapEl" v-loading="loadingRef" loading-tip="加载中...">
  3. <a-alert message="组件方式" />
  4. <a-button class="my-4 mr-4" type="primary" @click="openCompFullLoading">全屏 Loading</a-button>
  5. <a-button class="my-4" type="primary" @click="openCompAbsolute">容器内 Loading</a-button>
  6. <Loading :loading="loading" :absolute="absolute" :tip="tip" />
  7. <a-alert message="函数方式" />
  8. <a-button class="my-4 mr-4" type="primary" @click="openFnFullLoading">全屏 Loading</a-button>
  9. <a-button class="my-4" type="primary" @click="openFnWrapLoading">容器内 Loading</a-button>
  10. <a-alert message="指令方式" />
  11. <a-button class="my-4 mr-4" type="primary" @click="openDirectiveLoading">
  12. 打开指令Loading
  13. </a-button>
  14. </div>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, reactive, toRefs, ref } from 'vue';
  18. import { Loading, useLoading } from '/@/components/Loading';
  19. export default defineComponent({
  20. components: { Loading },
  21. setup() {
  22. const wrapEl = ref<ElRef>(null);
  23. const loadingRef = ref(false);
  24. const compState = reactive({
  25. absolute: false,
  26. loading: false,
  27. tip: '加载中...',
  28. });
  29. const [openFullLoading, closeFullLoading] = useLoading({
  30. tip: '加载中...',
  31. });
  32. const [openWrapLoading, closeWrapLoading] = useLoading({
  33. target: wrapEl,
  34. props: {
  35. tip: '加载中...',
  36. absolute: true,
  37. },
  38. });
  39. function openLoading(absolute: boolean) {
  40. compState.absolute = absolute;
  41. compState.loading = true;
  42. setTimeout(() => {
  43. compState.loading = false;
  44. }, 2000);
  45. }
  46. function openCompFullLoading() {
  47. openLoading(false);
  48. }
  49. function openCompAbsolute() {
  50. openLoading(true);
  51. }
  52. function openFnFullLoading() {
  53. openFullLoading();
  54. setTimeout(() => {
  55. closeFullLoading();
  56. }, 2000);
  57. }
  58. function openFnWrapLoading() {
  59. openWrapLoading();
  60. setTimeout(() => {
  61. closeWrapLoading();
  62. }, 2000);
  63. }
  64. function openDirectiveLoading() {
  65. loadingRef.value = true;
  66. setTimeout(() => {
  67. loadingRef.value = false;
  68. }, 2000);
  69. }
  70. return {
  71. openCompFullLoading,
  72. openFnFullLoading,
  73. openFnWrapLoading,
  74. openCompAbsolute,
  75. wrapEl,
  76. loadingRef,
  77. openDirectiveLoading,
  78. ...toRefs(compState),
  79. };
  80. },
  81. });
  82. </script>