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

This commit is contained in:
YunaiV 2023-03-10 21:24:20 +08:00
parent d989fa2081
commit 30dea302a9

View File

@ -40,6 +40,7 @@
</template>
<script setup lang="ts">
import * as ConfigApi from '@/api/infra/config'
const { t } = useI18n() //
const message = useMessage() //
@ -47,7 +48,7 @@ const modelVisible = ref(false) // 弹窗的是否展示
const modelTitle = ref('') //
const formLoading = ref(false) // 12
const formType = ref('') // create - update -
const formData = reactive({
const formData = ref({
id: undefined,
category: '',
name: '',
@ -75,9 +76,7 @@ const openModal = async (type: string, id?: number) => {
if (id) {
formLoading.value = true
try {
const data = await ConfigApi.getConfig(id)
// TODO reactive使 Object1 reactive ref
Object.assign(formData, data)
formData.value = await ConfigApi.getConfig(id)
} finally {
formLoading.value = false
}
@ -95,7 +94,7 @@ const submitForm = async () => {
//
formLoading.value = true
try {
const data = formData as ConfigApi.ConfigVO
const data = formData.value as ConfigApi.ConfigVO
if (formType.value === 'create') {
await ConfigApi.createConfig(data)
message.success(t('common.createSuccess'))
@ -104,6 +103,7 @@ const submitForm = async () => {
message.success(t('common.updateSuccess'))
}
modelVisible.value = false
//
emit('success')
} finally {
formLoading.value = false
@ -112,7 +112,7 @@ const submitForm = async () => {
/** 重置表单 */
const resetForm = () => {
Object.assign(formData, {
formData.value = {
id: undefined,
category: '',
name: '',
@ -120,7 +120,7 @@ const resetForm = () => {
value: '',
visible: true,
remark: ''
})
}
formRef.value?.resetFields()
}
</script>