index.vue 2.8 KB

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