const app = new Vue({ el: '#app', data: { massage: "测试", books: [{ id: 1, name: "《算法导论》", date: "2006-3", price: 85.00, count: 1 }, { id: 2, name: "《LUNIX编程艺术》", date: "2006-4", price: 185.00, count: 1 }, { id: 3, name: "《编程珠玑》", date: "2006-5", price: 15.00, count: 1 }, { id: 4, name: "《乡土中国》", date: "2006-6", price: 50.00, count: 1 }, ] }, //价格保留两位解决方案一:方法; price为参数,不确定 methods: { // getFinalPrice(price) { // return "¥" + price.toFixed(2) // } increment(index) { this.books[index].count++ }, decrement(index) { this.books[index].count-- }, remove(index) { this.books.splice(index, 1) } }, computed: { totolprice() { // 普通的for循环 let totolprice = 0 for (let i = 0; i < this.books.length; i++) { totolprice += this.books[i].price * this.books[i].count } return totolprice
} }, //价格保留两位解决方案二:过滤器的使用 filters: { showprice(price) { return "¥" + price.toFixed(2) } } })
|