1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| const vue = new Vue({ el: "#app", data: { books: [ { id: 1, name: "《算法导论》", date: "2006-9", price: 85.00, count: 1 }, { id: 2, name: "《UNIX编程艺术》", date: "2006-2", price: 59.00, count: 1 }, { id: 3, name: "《编程珠玑》", date: "2008-10", price: 39.00, count: 1 }, { id: 4, name: "《代码大全》", date: "2006-3", price: 128.00, count: 1 } ] },
filters: { showPrice(price) { return "¥" + parseFloat(price).toFixed(2); } },
methods: { increment(index) { this.books[index].count ++; }, decrement(index) { this.books[index].count --; }, remove(index){ this.books.splice(index, 1); } },
computed: { totalPrice() { let result = 0; for (let i=0; i<this.books.length; i++){ result += this.books[i].count * this.books[i].price; } return result; } } });
|