index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <PageWrapper title="modal组件使用示例">
  3. <Alert
  4. message="使用 useModal 进行弹窗操作,默认可以拖动,可以通过 draggable
  5. 参数进行控制是否可以拖动/全屏,并演示了在Modal内动态加载内容并自动调整高度"
  6. show-icon
  7. />
  8. <a-button type="primary" class="my-4" @click="openModalLoading"> 打开弹窗,加载动态数据并自动调整高度(默认可以拖动/全屏) </a-button>
  9. <Alert message="内外同时同时显示隐藏" show-icon />
  10. <a-button type="primary" class="my-4" @click="openModal2"> 打开弹窗</a-button>
  11. <Alert message="自适应高度" show-icon />
  12. <a-button type="primary" class="my-4" @click="openModal3"> 打开弹窗</a-button>
  13. <Alert message="内外数据交互" show-icon />
  14. <a-button type="primary" class="my-4" @click="send"> 打开弹窗并传递数据</a-button>
  15. <Alert message="使用动态组件的方式在页面内使用多个弹窗" show-icon />
  16. <a-space>
  17. <a-button type="primary" class="my-4" @click="openTargetModal(1)"> 打开弹窗1</a-button>
  18. <a-button type="primary" class="my-4" @click="openTargetModal(2)"> 打开弹窗2</a-button>
  19. <a-button type="primary" class="my-4" @click="openTargetModal(3)"> 打开弹窗3</a-button>
  20. <a-button type="primary" class="my-4" @click="openTargetModal(4)"> 打开弹窗4</a-button>
  21. </a-space>
  22. <component :is="currentModal" v-model:visible="modalVisible" :userData="userData" />
  23. <Modal1 @register="register1" :minHeight="100" />
  24. <Modal2 @register="register2" />
  25. <Modal3 @register="register3" />
  26. <Modal4 @register="register4" />
  27. </PageWrapper>
  28. </template>
  29. <script lang="ts">
  30. import { defineComponent, shallowRef, ComponentOptions, ref, nextTick } from 'vue';
  31. import { Alert, Space } from 'ant-design-vue';
  32. import { useModal } from '/@/components/Modal';
  33. import Modal1 from './Modal1.vue';
  34. import Modal2 from './Modal2.vue';
  35. import Modal3 from './Modal3.vue';
  36. import Modal4 from './Modal4.vue';
  37. import { PageWrapper } from '/@/components/Page';
  38. export default defineComponent({
  39. components: { Alert, Modal1, Modal2, Modal3, Modal4, PageWrapper, ASpace: Space },
  40. setup() {
  41. const currentModal = shallowRef<Nullable<ComponentOptions>>(null);
  42. const [register1, { openModal: openModal1 }] = useModal();
  43. const [register2, { openModal: openModal2 }] = useModal();
  44. const [register3, { openModal: openModal3 }] = useModal();
  45. const [register4, { openModal: openModal4 }] = useModal();
  46. const modalVisible = ref<Boolean>(false);
  47. const userData = ref<any>(null);
  48. function send() {
  49. openModal4(true, {
  50. data: 'content',
  51. info: 'Info',
  52. });
  53. }
  54. function openModalLoading() {
  55. openModal1(true);
  56. // setModalProps({ loading: true });
  57. // setTimeout(() => {
  58. // setModalProps({ loading: false });
  59. // }, 2000);
  60. }
  61. function openTargetModal(index) {
  62. switch (index) {
  63. case 1:
  64. currentModal.value = Modal1;
  65. break;
  66. case 2:
  67. currentModal.value = Modal2;
  68. break;
  69. case 3:
  70. currentModal.value = Modal3;
  71. break;
  72. default:
  73. currentModal.value = Modal4;
  74. break;
  75. }
  76. nextTick(() => {
  77. // `useModal` not working with dynamic component
  78. // passing data through `userData` prop
  79. userData.value = { data: Math.random(), info: 'Info222' };
  80. // open the target modal
  81. modalVisible.value = true;
  82. });
  83. }
  84. return {
  85. register1,
  86. openModal1,
  87. register2,
  88. openModal2,
  89. register3,
  90. openModal3,
  91. register4,
  92. openModal4,
  93. modalVisible,
  94. userData,
  95. openTargetModal,
  96. send,
  97. currentModal,
  98. openModalLoading,
  99. };
  100. },
  101. });
  102. </script>