src/composables/formConfirm.js
@@ -1,4 +1,4 @@
import { defineProps, onActivated, onDeactivated, ref, watch } from 'vue';
import { defineProps, onActivated, onDeactivated, reactive, ref, watch } from 'vue';
import { useCloned } from '@vueuse/core';
import { useMessageBoxTip, useMessageBox } from './messageBox';
@@ -13,17 +13,17 @@
  },
  reset = {
    do: () => {}
  }
  },
}) {
  if (!submit.title) submit.title = '提交';
  if (!submit.msg) submit.msg = '确认是否提交?';
  if (!cancel.title) cancel.title = '取消';
  if (!cancel.msg) cancel.msg = '是否放弃已编辑的内容?';
  const formProps = defineProps({
    // 是否在提交成功后清空表单
    clearAftSubmit: Boolean
  });
  // const formProps = defineProps({
  //   // 是否在提交成功后清空表单
  //   clearAftSubmit: Boolean
  // });
  //表单内容
  const formObj = ref(defaultForm ? defaultForm : {});
@@ -69,32 +69,33 @@
  // 重置表单
  const _reset = function () {
    formRef.value.clearValidate();
    edit.value = false;
    isReset = true;
    formObj.value = useCloned(formObjClone.cloned, {
      manual: true
    }).cloned.value;
    formRef.value.clearValidate();
  };
  // 清空表单
  const clear = function () {
    isReset = true;
    // formRef.value.resetFields();
    formRef.value.resetFields();
    edit.value = false;
  };
  // 提交成功后
  const submited = function () {
    if (formProps.clearAftSubmit) clear();
    // if (formProps.clearAftSubmit) clear();
    edit.value = false;
    formObjClone = useCloned(formObj, { manual: true });
  };
  // 提交表单
  const onSubmit = function () {
    formRef.value.validate((valid) => {
  const onSubmit = function (messageBox = true) {
    formRef.value.validate(async (valid) => {
      if (valid) {
        if (messageBox) {
        useMessageBoxTip({
          confirmMsg: submit.msg,
          confirmTitle: submit.title,
@@ -104,6 +105,10 @@
            return res;
          }
        });
        } else {
          await submit.do();
          submited();
        }
      }
    });
  };
@@ -147,5 +152,5 @@
    }
  };
  return { formProps, formObj, formRef, edit, onSubmit, onCancel, onReset };
  return { formObj, formRef, edit, onSubmit, onCancel, onReset, clear };
}