filters 过滤器

    1. filters: {
    2. showPrice(price) {
    3. return '¥' + price.toFixed(2)//保留2位小数
    4. }
    5. }

    image.png

    • index.html ```html <!DOCTYPE html>

      书籍名称 出版日期 价格 购买数量 操作
      {{item.id}} {{item.name}} {{item.date}} {{item.price | showPrice}} {{item.count}}

      总价格: {{totalPrice | showPrice}}

      购物车为空

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

    th, td { padding: 8px 16px; border: 1px solid #e9e9e9; text-align: left; }

    th { background-color: #f7f7f7; color: #5c6b77; font-weight: 600; } ```