期初数据前台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserList.vue 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div class="user-list">
  3. <h1>用户管理</h1>
  4. <div class="actions">
  5. <router-link to="/user/add">
  6. <button class="add-btn">添加用户</button>
  7. </router-link>
  8. </div>
  9. <table class="user-table">
  10. <thead>
  11. <tr>
  12. <th>ID</th>
  13. <th>用户名</th>
  14. <th>昵称</th>
  15. <th>邮箱</th>
  16. <th>电话</th>
  17. <th>状态</th>
  18. <th>操作</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <tr v-for="user in users" :key="user.id">
  23. <td>{{ user.id }}</td>
  24. <td>{{ user.username }}</td>
  25. <td>{{ user.nickname }}</td>
  26. <td>{{ user.email }}</td>
  27. <td>{{ user.phone }}</td>
  28. <td>{{ user.status === 1 ? '启用' : '禁用' }}</td>
  29. <td>
  30. <button @click="editUser(user.id)">编辑</button>
  31. <button @click="deleteUser(user.id)" class="delete-btn">删除</button>
  32. </td>
  33. </tr>
  34. </tbody>
  35. </table>
  36. </div>
  37. </template>
  38. <script setup>
  39. import { ref, onMounted } from 'vue'
  40. import { useRouter } from 'vue-router'
  41. import { getUsers, deleteUserById } from '../api/userApi'
  42. const router = useRouter()
  43. const users = ref([])
  44. const loadUsers = async () => {
  45. try {
  46. const response = await getUsers()
  47. users.value = response.data
  48. } catch (error) {
  49. console.error('获取用户列表失败:', error)
  50. }
  51. }
  52. const editUser = (id) => {
  53. router.push(`/user/edit/${id}`)
  54. }
  55. const deleteUser = async (id) => {
  56. if (confirm('确定要删除该用户吗?')) {
  57. try {
  58. await deleteUserById(id)
  59. loadUsers()
  60. } catch (error) {
  61. console.error('删除用户失败:', error)
  62. }
  63. }
  64. }
  65. onMounted(() => {
  66. loadUsers()
  67. })
  68. </script>
  69. <style scoped>
  70. .user-list {
  71. padding: 2rem;
  72. }
  73. .actions {
  74. margin-bottom: 1rem;
  75. text-align: right;
  76. }
  77. .add-btn {
  78. background-color: #646cff;
  79. color: white;
  80. }
  81. .user-table {
  82. width: 100%;
  83. border-collapse: collapse;
  84. }
  85. .user-table th,
  86. .user-table td {
  87. padding: 0.75rem;
  88. text-align: left;
  89. border-bottom: 1px solid #ddd;
  90. }
  91. .delete-btn {
  92. background-color: #ff4444;
  93. color: white;
  94. }
  95. button {
  96. margin-right: 0.5rem;
  97. }
  98. </style>