40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
new Vue({
|
|
el: "#app",
|
|
data: {
|
|
books: [
|
|
{ id: 871, name: "《西游记》", author: "吴承恩", num: 0 },
|
|
{ id: 652, name: "《红楼梦》", author: "曹雪芹", num: 0 },
|
|
{ id: 123, name: "《巴黎圣母院》", author: "雨果", num: 0 },
|
|
{ id: 014, name: "《悲惨世界》", author: "雨果", num: 0 },
|
|
{ id: 025, name: "《UNIX编程艺术》", author: "Eric S. Raymond", num: 0 },
|
|
{ id: 036, name: "《计算机导论》", author: "王志强", num: 0 },
|
|
{ id: 017, name: "《战争与和平》", author: "列夫·托尔斯泰", num: 0 },
|
|
{ id: 022, name: "《三国演义》", author: "罗贯中", num: 0 },
|
|
]
|
|
},
|
|
methods: {
|
|
buy_item(val) {
|
|
for (let i = 0; i < this.books.length; i++) {
|
|
if (val == this.books[i].id) {
|
|
this.books[i].num += 1
|
|
if (this.books[i].num > 10) {
|
|
this.books[i].num = 10
|
|
}
|
|
}
|
|
}
|
|
},
|
|
sale_item(val) {
|
|
for (let i = 0; i < this.books.length; i++) {
|
|
if (val == this.books[i].id) {
|
|
this.books[i].num -= 1
|
|
if (this.books[i].num <= 0) {
|
|
this.books[i].num = 0
|
|
}
|
|
}
|
|
}
|
|
},
|
|
remove(val) {
|
|
this.books.splice(val, 1)
|
|
}
|
|
},
|
|
}) |