修正了列表修改后fetchUsers的问题

This commit is contained in:
carry 2025-02-16 17:14:21 +08:00
parent 679bd25d7f
commit f7b44a9a17
2 changed files with 21 additions and 7 deletions

View File

@ -39,6 +39,7 @@ const props = defineProps<{
}>()
const dialogVisible = ref(false)
const options = ref<{ userId?: number, mode?: 'create' | 'edit', onConfirm?: () => void }>({})
const editForm = ref<UserResponse & { password?: string }>({
id: 0,
username: '',
@ -51,9 +52,10 @@ const editForm = ref<UserResponse & { password?: string }>({
const emit = defineEmits(['confirm'])
const open = async () => {
if (props.mode === 'edit' && props.userId) {
const user = await userService.getUser(props.userId)
const open = async (opts: { userId?: number, mode?: 'create' | 'edit', onConfirm?: () => void }) => {
options.value = opts
if (options.value.mode === 'edit' && options.value.userId) {
const user = await userService.getUser(options.value.userId)
editForm.value = { ...user }
} else {
editForm.value = {
@ -65,6 +67,9 @@ const open = async () => {
updated_at: ''
}
}
if (options.value.onConfirm) {
emit('confirm', options.value.onConfirm)
}
dialogVisible.value = true
}
@ -88,6 +93,9 @@ const handleConfirm = async () => {
await userService.updateUser(id, updateData)
}
emit('confirm', editForm.value)
if (options.value.onConfirm) {
options.value.onConfirm()
}
dialogVisible.value = false
} catch (error) {
console.error('操作失败:', error)

View File

@ -1,4 +1,7 @@
<template>
<div>
用户列表
</div>
<el-table :data="users" style="width: 100%">
<el-table-column prop="id" label="ID" width="100" />
<el-table-column prop="username" label="用户名" />
@ -37,7 +40,9 @@ const isAdmin = store.isAdmin;
const fetchUsers = async () => {
try {
users.value = await userService.getUsers();
const data = await userService.getUsers();
console.log('获取用户列表成功:', data);
users.value = data;
} catch (error) {
console.error('获取用户列表失败:', error);
}
@ -46,9 +51,10 @@ const fetchUsers = async () => {
const editDialogRef = ref<InstanceType<typeof EditUserDialog>>();
const handleEdit = (user: UserResponse) => {
editDialogRef.value?.open();
editDialogRef.value?.$emit('confirm', () => {
fetchUsers();
editDialogRef.value?.open({
userId: user.id,
mode: 'edit',
onConfirm: () => fetchUsers()
});
};