vue写一个学生管理带增删改查的项目
时间: 2023-06-19 08:07:12 浏览: 349
好的,以下是一个简单的学生管理系统的 Vue 代码示例,包括增删改查功能:
```html
<template>
<div>
<h1>学生管理系统</h1>
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(student, index) in students" :key="index">
<td>{{ student.id }}</td>
<td>{{ student.name }}</td>
<td>{{ student.grade }}</td>
<td>
<button @click="editStudent(index)">编辑</button>
<button @click="deleteStudent(index)">删除</button>
</td>
</tr>
</tbody>
</table>
<form>
<label>学号:<input type="text" v-model="newStudent.id"></label>
<label>姓名:<input type="text" v-model="newStudent.name"></label>
<label>年级:<input type="text" v-model="newStudent.grade"></label>
<button @click.prevent="addStudent">添加学生</button>
<button @click.prevent="updateStudent">更新学生</button>
</form>
</div>
</template>
<script>
export default {
data() {
return {
students: [
{ id: '001', name: '小明', grade: '一年级' },
{ id: '002', name: '小红', grade: '二年级' },
{ id: '003', name: '小刚', grade: '三年级' }
],
newStudent: { id: '', name: '', grade: '' },
selectedIndex: -1
}
},
methods: {
addStudent() {
this.students.push(this.newStudent)
this.newStudent = { id: '', name: '', grade: '' }
},
editStudent(index) {
this.newStudent = { ...this.students[index] }
this.selectedIndex = index
},
updateStudent() {
if (this.selectedIndex === -1) {
alert('请先选择要编辑的学生')
return
}
this.students.splice(this.selectedIndex, 1, this.newStudent)
this.newStudent = { id: '', name: '', grade: '' }
this.selectedIndex = -1
},
deleteStudent(index) {
this.students.splice(index, 1)
}
}
}
</script>
```
这是一个简单的示例,您可以根据您的需求进行修改和扩展。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)