用vue生成一个用户管理系统
时间: 2024-05-29 14:03:17 浏览: 133
好的,下面是一个简单的用户管理系统的代码示例,使用Vue.js和Element UI库进行开发:
首先,需要在HTML文件中引入Vue.js和Element UI库的CSS和JS文件:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户管理系统</title>
<link rel="stylesheet" href="//unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui/lib/index.js"></script>
<script src="main.js"></script>
</body>
</html>
```
然后,在main.js文件中编写Vue组件:
```javascript
Vue.component('user-table', {
data: function () {
return {
users: [
{ id: 1, name: '张三', email: 'zhangsan@example.com', phone: '13812345678' },
{ id: 2, name: '李四', email: 'lisi@example.com', phone: '13987654321' },
{ id: 3, name: '王五', email: 'wangwu@example.com', phone: '13666666666' },
],
columns: [
{ label: 'ID', prop: 'id' },
{ label: '姓名', prop: 'name' },
{ label: '邮箱', prop: 'email' },
{ label: '电话', prop: 'phone' },
{ label: '操作', slotName: 'actions' },
],
dialogVisible: false,
form: {
name: '',
email: '',
phone: '',
},
}
},
methods: {
addUser: function () {
this.dialogVisible = true
},
saveUser: function () {
this.users.push({
id: this.users.length + 1,
name: this.form.name,
email: this.form.email,
phone: this.form.phone,
})
this.dialogVisible = false
this.$message.success('添加成功!')
},
deleteUser: function (index) {
this.users.splice(index, 1)
this.$message.success('删除成功!')
},
},
template: `
<div>
<el-button type="primary" size="medium" @click="addUser">添加用户</el-button>
<el-table :data="users" stripe>
<el-table-column v-for="column in columns" :key="column.prop" :label="column.label">
<template v-if="column.slotName">
<el-table-column :key="column.slotName" :label="column.label" :sortable="column.sortable" width="150px">
<template slot-scope="scope">
<el-button type="text" size="small" @click="deleteUser(scope.$index)">删除</el-button>
</template>
</el-table-column>
</template>
<template v-else>
<el-table-column :prop="column.prop" :label="column.label" :sortable="column.sortable"></el-table-column>
</template>
</el-table-column>
</el-table>
<el-dialog title="添加用户" :visible.sync="dialogVisible">
<el-form label-position="left" label-width="80px">
<el-form-item label="姓名">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="邮箱">
<el-input v-model="form.email"></el-input>
</el-form-item>
<el-form-item label="电话">
<el-input v-model="form.phone"></el-input>
</el-form-item>
</el-form>
<div slot="footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="saveUser">保存</el-button>
</div>
</el-dialog>
</div>
`,
})
new Vue({
el: '#app',
})
```
最后,在HTML文件中使用Vue组件:
```html
<user-table></user-table>
```
这样,就可以在网页上看到一个简单的用户管理系统了。用户信息以表格形式展示,可以添加用户、删除用户,并且可以弹出对话框添加新用户。
阅读全文