用vue写个系统后台用户管理界面嘛
时间: 2023-11-06 07:04:09 浏览: 32
好的,我可以为您提供一个简单的系统后台用户管理界面的Vue实现。以下是代码示例:
```vue
<template>
<div>
<h2>用户管理</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>邮箱</th>
<th>角色</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" :key="user.id">
<td>{{ user.id }}</td>
<td>{{ user.username }}</td>
<td>{{ user.email }}</td>
<td>{{ user.role }}</td>
<td>
<button @click="editUser(user)">编辑</button>
<button @click="deleteUser(user)">删除</button>
</td>
</tr>
</tbody>
</table>
<button @click="addUser">添加用户</button>
<div v-if="showModal">
<h3>{{ modalTitle }}</h3>
<form @submit.prevent="submitForm">
<label for="username">用户名:</label>
<input type="text" v-model="form.username" required>
<br>
<label for="email">邮箱:</label>
<input type="email" v-model="form.email" required>
<br>
<label for="role">角色:</label>
<select v-model="form.role">
<option value="admin">管理员</option>
<option value="user">普通用户</option>
</select>
<br>
<button type="submit">{{ modalButton }}</button>
<button type="button" @click="closeModal">取消</button>
</form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
users: [
{ id: 1, username: 'admin', email: 'admin@example.com', role: 'admin' },
{ id: 2, username: 'user1', email: 'user1@example.com', role: 'user' },
{ id: 3, username: 'user2', email: 'user2@example.com', role: 'user' },
],
showModal: false,
modalTitle: '',
modalButton: '',
form: {
id: null,
username: '',
email: '',
role: 'user',
},
};
},
methods: {
addUser() {
this.modalTitle = '添加用户';
this.modalButton = '添加';
this.form.id = null;
this.form.username = '';
this.form.email = '';
this.form.role = 'user';
this.showModal = true;
},
editUser(user) {
this.modalTitle = '编辑用户';
this.modalButton = '保存';
this.form.id = user.id;
this.form.username = user.username;
this.form.email = user.email;
this.form.role = user.role;
this.showModal = true;
},
deleteUser(user) {
const index = this.users.indexOf(user);
if (index !== -1) {
this.users.splice(index, 1);
}
},
submitForm() {
if (this.form.id === null) {
// 新增用户
const id = this.users.length + 1;
this.users.push({ id, ...this.form });
} else {
// 编辑用户
const user = this.users.find(u => u.id === this.form.id);
if (user) {
user.username = this.form.username;
user.email = this.form.email;
user.role = this.form.role;
}
}
this.showModal = false;
},
closeModal() {
this.showModal = false;
},
},
};
</script>
```
这是一个简单的用户管理界面,包括用户列表、添加用户、编辑用户和删除用户等操作。您可以根据自己的需求对其进行扩展和优化。
注意,这只是一个示例,您需要根据自己的实际情况进行修改。同时,为了使代码更易读懂,我省略了一些必要的注释和错误处理。