Vue制作购物车(完整版,附带详细代码讲解)

 

目录

详细代码加注释

每日一句


详细代码加注释

<template>
  <div>
    <table>
      <thead>
        <tr>
          <td>
            <label>
              <!-- checked: 用于设定勾选状态 -->
              <input
                @change="checkAll"
                type="checkbox"
                :checked="isCheckAll"
              />
              全选
            </label>
          </td>
          <td>序号</td>
          <td>商品名</td>
          <td>单价</td>
          <td>数量</td>
          <td>小计</td>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(food, i) in foods" :key="i">
          <td>
            <!-- 推荐单标签闭合, 以前版本会报错, 如果不闭合 -->
            <input type="checkbox" v-model="food.checked" />
            {{ food.checked }}
          </td>
          <td>{{ i + 1 }}</td>
          <td>{{ food.name }}</td>
          <td>¥{{ food.price }}</td>
          <td>
            <button
              @click="food.count--"
              :disabled="food.count == 1"
            >
              -
            </button>
            <span>{{ food.count }}</span>
            <button @click="food.count++">+</button>
          </td>
          <td>¥{{ food.price * food.count }}</td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="5">总价格: ¥{{ getTotal }}</td>
          <!-- vue的自动化特色: 只要数据有变化, 相关的所有DOM元素都会刷新 -->
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
export default {
  // 事件触发的方法: methods
  methods: {
    // 如果事件在调用时,没写(), 则默认的参数1 是事件参数
    checkAll(e) {
      console.log(e.target.checked) //查看 checked 属性
      // 读取全选框的值, 赋值给每个商品的checked属性
      this.foods.forEach(
        food => (food.checked = e.target.checked)
      )
    },
  },

  computed: {
    //判断是否全选: 每一个的checked都是true
    isCheckAll() {
      return this.foods.every(food => food.checked)
    },
    getTotal() {
      // reduce: 让数组元素合并出一个值
      var a = this.foods.reduce((sum, food) => {
        const { price, count, checked } = food
        // *true 相当于 *1
        // *false 相当于 *0 = 0
        // 没有勾选, 就是 sum + 0, 即不累加
        return sum + price * count * checked
      }, 0)

      return a
    },
  },

  data() {
    return {
      foods: [
        { name: '茶碗蒸蛋', price: 9, count: 1, checked: true },
        { name: '小笼包', price: 12, count: 4, checked: false },
        { name: '豆腐脑', price: 6, count: 10, checked: true },
        { name: '豆浆', price: 4, count: 1, checked: false },
        { name: '油条', price: 2, count: 7, checked: true },
      ],
    }
  },
}
</script>

<style lang="scss" scoped>
table {
  border-collapse: collapse;

  thead {
    background-color: #eee;
  }
  td {
    padding: 10px 20px;
    border: 1px solid gray;

    span {
      display: inline-block;
      width: 40px;
      text-align: center;
    }
  }
}
</style>

每日一句

        "人不知而不愠,不亦君子乎?"

        "如果我有了某些成就,别人并不理解,可我决不会感到气愤、委屈。这不也是一种君子风度的表现吗?"

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的使用Vue.js制作购物车效果的示例: 首先,创建一个Vue实例: ``` var app = new Vue({ el: '#app', data: { products: [ { id: 1, name: 'iPhone X', price: 999.99, quantity: 0 }, { id: 2, name: 'iPad Pro', price: 799.99, quantity: 0 }, { id: 3, name: 'MacBook Pro', price: 1499.99, quantity: 0 } ], cart: [] }, methods: { addToCart: function(product) { if(product.quantity > 0) { var index = this.cart.findIndex(item => item.id === product.id); if(index >= 0) { this.cart[index].quantity += product.quantity; } else { this.cart.push({ id: product.id, name: product.name, price: product.price, quantity: product.quantity }); } product.quantity = 0; } }, removeFromCart: function(product) { var index = this.cart.findIndex(item => item.id === product.id); if(index >= 0) { this.products.find(item => item.id === product.id).quantity += this.cart[index].quantity; this.cart.splice(index, 1); } }, getTotal: function() { var total = 0; this.cart.forEach(item => { total += item.price * item.quantity; }); return total.toFixed(2); } } }); ``` 在这里,我们定义了一个包含商品和购物车的数据对象。商品包括ID、名称、价格和数量属性,而购物车仅包括ID、名称、价格和数量属性。我们还定义了三个方法: - addToCart:将商品添加到购物车中,然后将其数量重置为0。 - removeFromCart:从购物车中删除商品,并将其数量添加回商品列表中。 - getTotal:计算购物车中所有商品的总价值。 接下来,我们可以在Vue模板中使用这些数据和方法来创建购物车效果: ``` <div id="app"> <div class="product-list"> <div v-for="product in products" :key="product.id"> <h3>{{ product.name }}</h3> <p>{{ product.price | currency }}</p> <input type="number" v-model.number="product.quantity" min="0" max="10"> <button @click="addToCart(product)">Add to Cart</button> </div> </div> <div class="cart"> <h2>Shopping Cart</h2> <div v-for="product in cart" :key="product.id"> <h3>{{ product.name }}</h3> <p>{{ product.price | currency }}</p> <input type="number" v-model.number="product.quantity" min="0" max="10"> <button @click="removeFromCart(product)">Remove from Cart</button> </div> <p>Total: {{ getTotal() | currency }}</p> </div> </div> ``` 在这里,我们使用v-for指令来遍历商品列表和购物车,并使用v-model指令来绑定商品数量输入框和购物车数量输入框。我们还使用过滤器来格式化价格和总价值。最后,我们使用@click指令来触发添加和删除商品的事件。 这是一个简单的购物车示例,你可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张的俊.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值