11<template >
22 <!-- 弹窗 -->
3- <XModal id = " PostForm " :loading =" modelLoading" v-model =" modelVisible" :title = " modelTitle " >
3+ <XModal :title = " modelTitle " :loading =" modelLoading" v-model =" modelVisible" height = " 270px " >
44 <!-- 表单:添加/修改 -->
55 <Form
66 ref =" formRef"
@@ -35,22 +35,21 @@ import { rules, allSchemas } from './post.data'
3535const { t } = useI18n () // 国际化
3636const message = useMessage () // 消息弹窗
3737
38- const emit = defineEmits ([' success' ])
39-
4038// 弹窗相关的变量
4139const modelVisible = ref (false ) // 是否显示弹出层
42- const modelTitle = ref (' update ' ) // 弹出层标题
40+ const modelTitle = ref (' ' ) // 弹出层标题
4341const modelLoading = ref (false ) // 弹出层loading
4442const actionType = ref (' ' ) // 操作按钮的类型
4543const actionLoading = ref (false ) // 按钮 Loading
4644const formRef = ref <FormExpose >() // 表单 Ref
4745const detailData = ref () // 详情 Ref
4846
47+ // 打开弹窗
4948const openModal = async (type : string , rowId ? : number ) => {
49+ modelVisible .value = true
5050 modelLoading .value = true
5151 modelTitle .value = t (' action.' + type )
5252 actionType .value = type
53- modelVisible .value = true
5453 // 设置数据
5554 if (rowId ) {
5655 const res = await PostApi .getPostApi (rowId )
@@ -62,31 +61,33 @@ const openModal = async (type: string, rowId?: number) => {
6261 }
6362 modelLoading .value = false
6463}
64+ defineExpose ({ openModal: openModal }) // 提供 openModal 方法,用于打开弹窗
6565
6666// 提交新增/修改的表单
67+ const emit = defineEmits ([' success' ]) // 定义 success 事件,用于操作成功后的回调
6768const submitForm = async () => {
69+ // 校验表单
6870 const elForm = unref (formRef )?.getElFormRef ()
6971 if (! elForm ) return
7072 const valid = await elForm .validate ()
71- if (valid ) {
72- actionLoading .value = true
73- // 提交请求
74- try {
75- const data = unref (formRef )?.formModel as PostApi .PostVO
76- if (actionType .value === ' create' ) {
77- await PostApi .createPostApi (data )
78- message .success (t (' common.createSuccess' ))
79- } else {
80- await PostApi .updatePostApi (data )
81- message .success (t (' common.updateSuccess' ))
82- }
83- modelVisible .value = false
84- emit (' success' )
85- } finally {
86- actionLoading .value = false
73+ if (! valid ) {
74+ return
75+ }
76+ // 提交请求
77+ actionLoading .value = true
78+ try {
79+ const data = unref (formRef )?.formModel as PostApi .PostVO
80+ if (actionType .value === ' create' ) {
81+ await PostApi .createPostApi (data )
82+ message .success (t (' common.createSuccess' ))
83+ } else {
84+ await PostApi .updatePostApi (data )
85+ message .success (t (' common.updateSuccess' ))
8786 }
87+ modelVisible .value = false
88+ emit (' success' )
89+ } finally {
90+ actionLoading .value = false
8891 }
8992}
90-
91- defineExpose ({ openModal: openModal })
9293 </script >
0 commit comments