vue实现购物车功能

​​
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    table {
      border: 1px solid #e9e9e9;
      border-spacing: 0;
      border-collapse: collapse;
    }

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

    th {
      background-color: #f7f7f7;
      color: #5c6d77;
    }
  </style>
</head>

<body>
  <div id="app">
    <div v-if="books.length">
      <table>
        <thead>
          <!-- 循环表头 -->
          <th v-for="item in shopTitle">{{item}}</th>
        </thead>
        <tbody>
          <!-- 循环每一项 -->
          <tr v-for="(item,index) in books">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date}}</td>
            <td>{{item.price | showPrice}}</td>
            <td>
              <!-- 减的时候,当数量<=1时不能点击 传入index是为了确定当前点击的减的项是哪一个-->
              <button @click="discrement(index)" :disabled="item.count<=1">-</button>
              {{item.count}}
              <button @click="increment(index)">+</button>
            </td>
            <td>
              <button @click="removeHandle(index)">移除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <!-- 这里使用计算属性=====是因为他是动态变化的值 -->
      <h2>总价:{{totalPrice}}</h2>
    </div>
    <div v-else>
      <h2>购物车当前为空,快去添加心仪的产品吧</h2>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        // 模拟数据
        shopTitle: ['', '书籍名称', '出版日期', '价格', '购买数量', '操作'],
        books: [{
          id: 1,
          name: '《JavaScript高级程序设计(第三版)》',
          date: "2012-6",
          price: 99.00,
          count: 1
        },
        {
          id: 2,
          name: '《JavaScript高级程序设计(第三版)》',
          date: "2012-6",
          price: 99.00,
          count: 1
        },
        {
          id: 3,
          name: '《JavaScript高级程序设计(第三版)》',
          date: "2012-6",
          price: 99.00,
          count: 1
        },
        {
          id: 4,
          name: '《JavaScript高级程序设计(第三版)》',
          date: "2012-6",
          price: 99.00,
          count: 1
        }]
      },
      methods: {
        // 数量每次减一
        discrement(index) {
          this.books[index].count--;
        },
        // 数量每次加一
        increment(index) {
          this.books[index].count++;
        },
        // 删除当前点击的数组的一个
        removeHandle(index) {
          this.books.splice(index, 1)
        }
      },
      // 价格求和
      filters: {
        showPrice(price) {
          return '¥' + price.toFixed(2)//保留数值为小数点后两位的值,赋值为新的值,前面是字符串拼接
        }
      },
      // 计算属性---使用时不需要加小括号
      computed: {
        totalPrice() {
          let totalPrice = 0
          // for in 方法
          // for (let i in this.books) {
          //   totalPrice += this.books[i].price * this.books[i].count;
          //   console.log(i);
          // }

          // for of 方法
          for (let i of this.books) {
            console.log(i); //可以看一下i打印的是什么
            totalPrice += i.price * i.count
          }
          return totalPrice
        }
      }
    })
  </script>
</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值