重构:调整 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> </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) // 弹窗的是否展示
const modelTitle = ref('') // const modelTitle = ref('') //
const formLoading = ref(false) // 12 const formLoading = ref(false) // 12
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) => {
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使 Object1 reactive ref
Object.assign(formData, data)
} finally { } finally {
formLoading.value = false formLoading.value = false
} }
@ -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 () => {
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 () => {
/** 重置表单 */ /** 重置表单 */
const resetForm = () => { const resetForm = () => {
Object.assign(formData, { formData.value = {
id: undefined, id: undefined,
category: '', category: '',
name: '', name: '',
@ -120,7 +120,7 @@ const resetForm = () => {
value: '', value: '',
visible: true, visible: true,
remark: '' remark: ''
}) }
formRef.value?.resetFields() formRef.value?.resetFields()
} }
</script> </script>