vue实现图书管理案例,具体功能要求如下: 1. 动态在页面中展示图书列表; 2. 在输入框中输入图书名称,按回车键实现图书添加功能(图书名称不能为空); 3. 点击“删除"按钮可以实现删除图书
时间: 2024-02-11 08:08:36 浏览: 23
好的,下面是一个简单的实现图书管理的案例,具体功能如下:
1. 在页面中展示图书列表;
2. 在输入框中输入图书名称,按回车键实现图书添加功能(图书名称不能为空);
3. 点击“删除”按钮可以实现删除图书。
代码如下:
```
<template>
<div>
<h2>图书管理</h2>
<div>
<input type="text" v-model="newBookName" placeholder="请输入图书名称" @keyup.enter="addBook">
<button @click="addBook">添加</button>
</div>
<table>
<thead>
<tr>
<th>序号</th>
<th>图书名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(book, index) in bookList" :key="book.id">
<td>{{ index + 1 }}</td>
<td>{{ book.name }}</td>
<td><button @click="deleteBook(index)">删除</button></td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
bookList: [
{ id: 1, name: 'Vue.js 实战' },
{ id: 2, name: 'JavaScript 高级程序设计' },
{ id: 3, name: 'CSS 世界' }
],
newBookName: ''
}
},
methods: {
addBook() {
if (this.newBookName.trim() === '') {
return
}
const id = this.bookList.length > 0 ? this.bookList[this.bookList.length - 1].id + 1 : 1
this.bookList.push({ id, name: this.newBookName })
this.newBookName = ''
},
deleteBook(index) {
this.bookList.splice(index, 1)
}
}
}
</script>
```
这个组件中包含了一个输入框、一个添加按钮和一个表格,其中表格中列出了当前的图书列表。当用户在输入框中输入图书名称并按下回车键或点击添加按钮时,会将输入的图书添加到图书列表中。当用户点击删除按钮时,会将对应的图书从列表中删除。