1
1
<template >
2
2
<Dialog :title =" modelTitle" v-model =" modelVisible" >
3
3
<el-form
4
- ref =" ruleFormRef "
4
+ ref =" formRef "
5
5
:model =" formData"
6
6
:rules =" formRules"
7
7
label-width =" 80px"
33
33
<template #footer >
34
34
<div class =" dialog-footer" >
35
35
<el-button @click =" submitForm" type =" primary" :disabled =" formLoading" >确 定</el-button >
36
- <el-button @click =" colseForm(ruleFormRef) " >取 消</el-button >
36
+ <el-button @click =" modelVisible = false " >取 消</el-button >
37
37
</div >
38
38
</template >
39
39
</Dialog >
40
40
</template >
41
41
<script setup lang="ts">
42
42
import * as ConfigApi from ' @/api/infra/config'
43
- // import type { FormExpose } from '@/components/Form'
44
- import type { FormInstance } from ' element-plus'
45
43
const { t } = useI18n () // 国际化
46
44
const message = useMessage () // 消息弹窗
47
- // const { proxy } = getCurrentInstance()
48
45
49
46
const modelVisible = ref (false ) // 弹窗的是否展示
50
47
const modelTitle = ref (' ' ) // 弹窗的标题
51
48
const formLoading = ref (false ) // 表单的 Loading 加载:1)修改时的数据加载;2)提交的按钮禁用
52
49
const formType = ref (' ' ) // 表单的类型:create - 新增;update - 修改
53
- // let formRef = ref() // 表单的 Ref
54
50
const formData = reactive ({
55
51
id: undefined ,
56
52
category: ' ' ,
@@ -67,9 +63,7 @@ const formRules = reactive({
67
63
value: [{ required: true , message: ' 参数键值不能为空' , trigger: ' blur' }],
68
64
visible: [{ required: true , message: ' 是否可见不能为空' , trigger: ' blur' }]
69
65
})
70
- let ruleFormRef = ref <FormInstance >() // 表单 Ref
71
-
72
- const { proxy } = getCurrentInstance () as any
66
+ const formRef = ref () // 表单 Ref
73
67
74
68
/** 打开弹窗 */
75
69
const openModal = async (type : string , id ? : number ) => {
@@ -94,15 +88,9 @@ defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗
94
88
/** 提交表单 */
95
89
const emit = defineEmits ([' success' ]) // 定义 success 事件,用于操作成功后的回调
96
90
const submitForm = async () => {
97
- if (true ) {
98
- formLoading .value = true
99
- console .log (' 1111' )
100
- return
101
- }
102
- const formRef = proxy .$refs [' formRef' ]
103
91
// 校验表单
104
92
if (! formRef ) return
105
- const valid = await formRef .validate ()
93
+ const valid = await formRef .value . validate ()
106
94
if (! valid ) return
107
95
// 提交请求
108
96
formLoading .value = true
@@ -124,27 +112,15 @@ const submitForm = async () => {
124
112
125
113
/** 重置表单 */
126
114
const resetForm = () => {
127
- formData .id = undefined
128
- formData .category = ' '
129
- formData .name = ' '
130
- formData .key = ' '
131
- formData .value = ' '
132
- formData .visible = true
133
- formData .remark = ' '
134
- // 重置表单
135
- console .log (ruleFormRef )
136
- console .log (ruleFormRef .value )
137
- // proxy.$refs['ruleFormRef'].resetFields()
138
- // setTimeout(() => {
139
- // console.log(ruleFormRef.value, 'formRef.value')
140
- // formRef.value.resetFields() // TODO 芋艿:为什么拿不到 formRef 呢? 表单还没加载出来
141
- // ruleFormRef.value?.resetFields()
142
- // }, 100)
143
- }
144
- /** 取消添加 */
145
- const colseForm = (formEl : FormInstance | undefined ) => {
146
- if (! formEl ) return
147
- formEl .resetFields ()
148
- modelVisible .value = false
115
+ Object .assign (formData , {
116
+ id: undefined ,
117
+ category: ' ' ,
118
+ name: ' ' ,
119
+ key: ' ' ,
120
+ value: ' ' ,
121
+ visible: true ,
122
+ remark: ' '
123
+ })
124
+ formRef .value ?.resetFields ()
149
125
}
150
126
</script >
0 commit comments