modal.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { ModalName } from '@/common/constants/modalName';
  2. import { inject, provide, ref, watchEffect } from 'vue';
  3. // 控制弹窗
  4. /**
  5. * 控制关闭弹窗
  6. * @param modelName 相对应的弹窗组件名
  7. * @returns
  8. */
  9. export function closeModal(modelName: keyof ModalName) {
  10. // 控制弹窗 flag
  11. const visible = ref<boolean>(false);
  12. const show = inject(modelName, ref<boolean>(false))
  13. watchEffect(() => {
  14. visible.value = show.value
  15. })
  16. /**
  17. * 取消弹窗
  18. */
  19. function cancel() {
  20. visible.value = false;
  21. show.value = false
  22. }
  23. return { visible, cancel }
  24. }
  25. /**
  26. * 控制打开弹窗
  27. * @param modelName 相对应的弹窗组件名
  28. * @returns openAction 打开弹窗方法
  29. */
  30. export function openModal(modelName: keyof ModalName) {
  31. const name = ref<boolean>(false)
  32. provide(modelName, name);
  33. /**
  34. * 打开弹窗
  35. * @param value 弹窗名
  36. */
  37. function openAction() {
  38. console.log('弹窗组件名', modelName);
  39. name.value = true;
  40. }
  41. return { openAction }
  42. }
  43. export type { ModalName };