axios.defaults.baseURL = 'http://localhost:8080/book/'; var vm = new Vue({ el: '#app', data: { id: '', bookName:'', flag: false, submitFlag: false, books: [] }, methods:{ handle: async function () { //编辑 }, //编辑处理 toEdit: async function (id) { }, deleteBook: async function (id) { //第一种方法 /*var index = this.books.findIndex(function (item) { return item.id == this.id; }); this.books.splice(index,1);*/ // 第二种方法:通过filter方法进行删除 /*this.books = this.books.filter(function (item) { return item.id !== id; });*/ }, queryBooks: async function () { } }, computed:{ total: function () { } }, watch: { name: function (val) { } }, mounted:function () { } });请完善代码
时间: 2024-02-10 16:17:04 浏览: 27
根据您的代码,我看到您正在使用Vue.js和axios来构建一个图书管理系统前端页面。下面是完善后的代码:
```
axios.defaults.baseURL = 'http://localhost:8080/book/';
var vm = new Vue({
el: '#app',
data: {
id: '',
bookName:'',
flag: false,
submitFlag: false,
books: []
},
methods:{
handle: async function () {
//编辑
var book = {
id: this.id,
bookName: this.bookName
};
await axios.put(book.id, book);
await this.queryBooks();
this.id = '';
this.bookName = '';
this.flag = false;
},
//编辑处理
toEdit: async function (id) {
var response = await axios.get(id);
this.id = response.data.id;
this.bookName = response.data.bookName;
this.flag = true;
},
deleteBook: async function (id) {
//第一种方法
/*var index = this.books.findIndex(function (item) {
return item.id == this.id;
});
this.books.splice(index,1);*/
//第二种方法:通过filter方法进行删除
/*this.books = this.books.filter(function (item) {
return item.id !== id;
});*/
await axios.delete(id);
await this.queryBooks();
},
queryBooks: async function () {
var response = await axios.get();
this.books = response.data;
}
},
computed:{
total: function () {
return this.books.length;
}
},
watch: {
name: function (val) {
console.log(val);
}
},
mounted:function () {
this.queryBooks();
}
});
```
这个代码中,我添加了一些新的方法和功能:
1. `handle()` 方法用于编辑图书信息,它发送 PUT 请求到后端 API 来更新图书信息,并且在更新后重新查询所有的图书信息。
2. `toEdit()` 方法用于在编辑按钮被点击时,获取特定的图书信息并在表单里面显示出来。
3. `deleteBook()` 方法用于删除特定的图书信息,它发送 DELETE 请求到后端 API 并且在删除成功后重新查询所有的图书信息。
4. `queryBooks()` 方法用于查询所有的图书信息,它发送 GET 请求到后端 API 并且将结果保存到 `books` 数组中。
5. `mounted()` 生命周期钩子用于在页面加载时调用 `queryBooks()` 方法来初始化所有的图书信息。
希望这个完善后的代码可以帮助您完成您的图书管理系统。