用vue+elementUI写一个用户管理页面
时间: 2023-12-16 18:06:24 浏览: 85
好的,我可以为您提供一些思路和代码示例。首先,我们需要使用Vue.js和Element UI库来搭建用户管理页面。以下是一个简单的示例代码:
```html
<template>
<div class="user-management">
<el-table :data="users" border>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column prop="gender" label="性别"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="primary" size="small" @click="editUser(scope.row)">编辑</el-button>
<el-button type="danger" size="small" @click="deleteUser(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog :visible.sync="dialogVisible" title="编辑用户">
<el-form ref="userForm" :model="currentUser" label-width="80px">
<el-form-item label="姓名" prop="name">
<el-input v-model="currentUser.name"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input-number v-model="currentUser.age"></el-input-number>
</el-form-item>
<el-form-item label="性别" prop="gender">
<el-radio-group v-model="currentUser.gender">
<el-radio label="male">男</el-radio>
<el-radio label="female">女</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="地址" prop="address">
<el-input v-model="currentUser.address"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="saveUser">保存</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { mapActions } from 'vuex'
export default {
data() {
return {
users: [
{ name: '张三', age: 20, gender: 'male', address: '北京市' },
{ name: '李四', age: 25, gender: 'female', address: '上海市' },
{ name: '王五', age: 30, gender: 'male', address: '广州市' },
],
dialogVisible: false,
currentUser: {},
}
},
methods: {
...mapActions(['addUser', 'updateUser', 'deleteUser']),
editUser(user) {
this.currentUser = { ...user }
this.dialogVisible = true
},
saveUser() {
this.$refs.userForm.validate(valid => {
if (valid) {
if (this.currentUser.id) {
this.updateUser(this.currentUser)
} else {
this.addUser(this.currentUser)
}
this.dialogVisible = false
}
})
},
deleteUser(user) {
this.deleteUser(user.id)
},
},
}
</script>
```
在上面的代码中,我们使用了Element UI的`el-table`和`el-dialog`组件来展示用户数据和编辑用户信息。我们还使用了Vue.js的`v-model`指令来双向绑定表单数据。另外,我们还引入了Vuex来处理用户数据的增删改操作。需要注意的是,上面的代码只是一个简单的示例,您可以根据实际需求进行修改和扩展。
阅读全文