Vue实例:购物车功能完善

学习视频连接:https://www.bilibili.com/video/BV15741177Eh?p=40

一、先设置价格的格式

发现价格显示的信息不是很完善。

添加价格过滤器:

  filters: {  
    showPrice (price){  //价格过滤器
      return '¥' + price.toFixed(2);
    }
  }

在html中使用过滤器

代码有省略,只给出相关部分。

<td>{{book.price | showPrice}}</td>
<p>总价格:{{totalPrice | showPrice}}</p>

二、设置购买数量和移除按键点击响应

这里再传入对象时有几种方式:
目的都一样:只添加或减少点击的对象的数量,不改变其它对象数量。同时设置当数量减少到小于等于1时,减少按钮不能再点击。用到了v-bind动态绑定设置disabled状态。

  • 可以传入books对象的index,然后再通过this.books[index].count来进行数量的加减
  • 可以直接传一个book对象

移除按钮使用了splice函数,可以做到响应式

 methods: {
    decrement(book){
      book.count--;
    },
    increment (book){
      book.count++;
    },
    remove (index){
      this.books.splice(index, 1);
    }
  },
   <td>
     <button @click="decrement(book)" v-bind:disabled="book.count <= 1">-</button>
     {{book.count}}
     <button @click="increment(book)">+</button>
   </td>
   <td><button @click="remove(index)">移除</button></td>

三、计算购物车的总价格

很容易想到这里该使用计算属性

  • for循环中最好使用let,涉及到let的作用域
  computed: {
    totalPrice(){
      let totalPrice = 0;
      for (let i = 0; i < this.books.length; i++){
        totalPrice += this.books[i].price * this.books[i].count;
      }
      return totalPrice;
      // 下面是简单写法
      // return this.books.reduce(function (preValue, book) {
      //   return preValue + book.price * book.count;
      // }, 0);
    }
  },
<p>总价格:{{totalPrice | showPrice}}</p>

效果

在这里插入图片描述

所有代码:

<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <title>购物车</title>
  <link rel="stylesheet" href="style.css">
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>

  <div id="app">
    <div v-if="books.length">
      <table>
        <thead>
          <tr>
            <th></th>
            <th>书籍名称</th>
            <th>出版日期</th>
            <th>价格</th>
            <th>购买数量</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(book, index) in books">
            <td>{{book.id}}</td>
            <td>{{book.name}}</td>
            <td>{{book.date}}</td>
            <td>{{book.price | showPrice}}</td>
            <td>
              <button @click="decrement(book)" v-bind:disabled="book.count <= 1">-</button>
              {{book.count}}
              <button @click="increment(book)">+</button>
            </td>
            <td><button @click="remove(index)">移除</button></td>
          </tr>
        </tbody>
      </table>
      <p>总价格:{{totalPrice | showPrice}}</p>
    </div>
    <p v-else>购物车为空</p>
  </div>

  <script src="main.js"></script>
</body>

</html>
const app = new Vue({
  el: '#app',
  data: {
    books: [
      {
        id: 1,
        name: '《算法导论》',
        date: '2006-01',
        price: 85.00,
        count: 1
      },
      {
        id: 2,
        name: '《UNIX编程艺术》',
        date: '2008-12',
        price: 59.00,
        count: 1
      },
      {
        id: 3,
        name: '《编程珠玑》',
        date: '2010-12',
        price: 39.00,
        count: 1
      },
      {
        id: 4,
        name: '《代码大全》',
        date: '2012-12',
        price: 128.00,
        count: 1
      }
    ]
  },
  methods: {
    decrement(book){
      book.count--;
    },
    increment (book){
      book.count++;
    },
    remove (index){
      this.books.splice(index, 1);
    }
  },
  computed: {
    totalPrice(){
      let totalPrice = 0;
      for (let i = 0; i < this.books.length; i++){
        totalPrice += this.books[i].price * this.books[i].count;
      }
      return totalPrice;
    }
  },
  filters: {  
    showPrice (price){  //价格过滤器
      return '¥' + price.toFixed(2);
    }
  }
})
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;
}

猜你喜欢

转载自blog.csdn.net/weixin_43207025/article/details/107216817