Commit 30dea302 by YunaiV

重构:调整 formData 使用 ref。原因是,原本的 reactive 无法整个赋值

parent d989fa20
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import * as ConfigApi from '@/api/infra/config' import * as ConfigApi from '@/api/infra/config'
const { t } = useI18n() // 国际化 const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗 const message = useMessage() // 消息弹窗
...@@ -47,7 +48,7 @@ const modelVisible = ref(false) // 弹窗的是否展示 ...@@ -47,7 +48,7 @@ const modelVisible = ref(false) // 弹窗的是否展示
const modelTitle = ref('') // 弹窗的标题 const modelTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
const formType = ref('') // 表单的类型:create - 新增;update - 修改 const formType = ref('') // 表单的类型:create - 新增;update - 修改
const formData = reactive({ const formData = ref({
id: undefined, id: undefined,
category: '', category: '',
name: '', name: '',
...@@ -75,9 +76,7 @@ const openModal = async (type: string, id?: number) => { ...@@ -75,9 +76,7 @@ const openModal = async (type: string, id?: number) => {
if (id) { if (id) {
formLoading.value = true formLoading.value = true
try { try {
const data = await ConfigApi.getConfig(id) formData.value = await ConfigApi.getConfig(id)
// TODO 规范纠结点:因为用 reactive,所以需要使用 Object;可以替换的方案,1)把 reactive 改成 ref;
Object.assign(formData, data)
} finally { } finally {
formLoading.value = false formLoading.value = false
} }
...@@ -95,7 +94,7 @@ const submitForm = async () => { ...@@ -95,7 +94,7 @@ const submitForm = async () => {
// 提交请求 // 提交请求
formLoading.value = true formLoading.value = true
try { try {
const data = formData as ConfigApi.ConfigVO const data = formData.value as ConfigApi.ConfigVO
if (formType.value === 'create') { if (formType.value === 'create') {
await ConfigApi.createConfig(data) await ConfigApi.createConfig(data)
message.success(t('common.createSuccess')) message.success(t('common.createSuccess'))
...@@ -104,6 +103,7 @@ const submitForm = async () => { ...@@ -104,6 +103,7 @@ const submitForm = async () => {
message.success(t('common.updateSuccess')) message.success(t('common.updateSuccess'))
} }
modelVisible.value = false modelVisible.value = false
// 发送操作成功的事件
emit('success') emit('success')
} finally { } finally {
formLoading.value = false formLoading.value = false
...@@ -112,7 +112,7 @@ const submitForm = async () => { ...@@ -112,7 +112,7 @@ const submitForm = async () => {
/** 重置表单 */ /** 重置表单 */
const resetForm = () => { const resetForm = () => {
Object.assign(formData, { formData.value = {
id: undefined, id: undefined,
category: '', category: '',
name: '', name: '',
...@@ -120,7 +120,7 @@ const resetForm = () => { ...@@ -120,7 +120,7 @@ const resetForm = () => {
value: '', value: '',
visible: true, visible: true,
remark: '' remark: ''
}) }
formRef.value?.resetFields() formRef.value?.resetFields()
} }
</script> </script>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment