<template>
  <div class='container'>
    <div class='cart-list'>
<pre><code>  &lt;div v-if='cartList.length &lt; 1'&gt;
    &lt;h1&gt;购物车空空的,快去购物吧&lt;/h1&gt;
  &lt;/div&gt;

  &lt;el-row v-if='cartList.length &gt; 0'&gt;
    &lt;el-col&gt;
      &lt;el-card :body-style='{ padding: '0px' }'&gt;
        &lt;div class='cart-list-header'&gt;
          &lt;span style='font-size: 22px'&gt;购物车(全部&lt;span&gt;{{ cartList.length }}&lt;/span&gt;)&lt;/span&gt;
          &lt;el-checkbox v-model='allCheck' class='all-check' @change='changeAllCheck'&gt;全选&lt;/el-checkbox&gt;
          &lt;el-button type='warning' @click='removeMany'&gt;移除&lt;/el-button&gt;
          &lt;el-button type='danger' @click='buyMany'&gt;购买&lt;/el-button&gt;
        &lt;/div&gt;
      &lt;/el-card&gt;
    &lt;/el-col&gt;

    &lt;el-col :span='24' v-for='(product,index) in cartList.slice().reverse()' :key='index'&gt;
      &lt;el-card :body-style='{ padding: '0px' }'&gt;
        &lt;div class='cart-item'&gt;
          &lt;el-checkbox v-model='product.selected' class='checkedBox' @change='singleSelect'&gt;&lt;/el-checkbox&gt;
          &lt;img :src='require('@/assets/goods/'+product.image)' class='product-image' alt=''&gt;
          &lt;div class='product-title'&gt;{{ product.title }}&lt;/div&gt;

          &lt;div class='product-item-quantity'&gt;
            &lt;span class='product-quantity-title'&gt;数量:&lt;/span&gt;
            &lt;el-input-number v-model='product.quantity' size='mini' :min='1' :max='product.stock' @change='quantityChange(product)' :step='1'&gt;&lt;/el-input-number&gt;
          &lt;/div&gt;

          &lt;div class='product-item-size'&gt;
            &lt;span class='product-quantity-title'&gt;尺码:&lt;/span&gt;
            &lt;span&gt;{{product.size}}&lt;/span&gt;
          &lt;/div&gt;

          &lt;div class='product-item-price'&gt;
            &lt;span class='product-price-title'&gt;单价:&lt;/span&gt;
            &lt;span class='product-price'&gt;¥{{ product.price }}&lt;/span&gt;
          &lt;/div&gt;

          &lt;div class='product-item-price'&gt;
            &lt;span class='product-price-title'&gt;总价:&lt;/span&gt;
            &lt;span class='product-price'&gt;¥{{ product.amount }}&lt;/span&gt;
          &lt;/div&gt;

          &lt;el-button type='warning' @click='removeOne(index)'&gt;移除&lt;/el-button&gt;
          &lt;el-button type='danger' @click='buyOne(product.id)'&gt;购买&lt;/el-button&gt;
        &lt;/div&gt;
      &lt;/el-card&gt;
    &lt;/el-col&gt;
  &lt;/el-row&gt;

&lt;/div&gt;
</code></pre>
  </div>
</template>
<script>
import { getList } from '@/api/cart'
import { Message } from 'element-ui'

export default {
  data() {
    return {
      allCheck: false,
      minCount: 1,
      cartList: [],
    }
  },
  created() {
    this.getCartList()
  },
  methods: {
    getCartList() {
      this.cartList = getList()
    },
    // 数量改变
    quantityChange(product) {
      product.amount = (product.quantity * product.price).toFixed(2)
    },

    // 全选
    changeAllCheck() {
      if (this.allCheck) {
        this.cartList.forEach(item => {
          item.selected = true
        })
      } else {
        this.cartList.forEach(item => {
          item.selected = false
        })
      }
    },

    // 单选
    singleSelect() {
      let checkCount = 0
      this.cartList.forEach(item => {
        if (item.selected) {
          checkCount++
        }
      })
      if (checkCount === this.cartList.length) {
        this.allCheck = true
      } else {
        this.allCheck = false
      }
    },

    // 单个移出购物车
    removeOne(index) {
      this.cartList.splice(index, 1)

      Message({
        message: '移出成功',
        type: 'success',
        showClose: true,
        duration: 3000
      })
    },

    // 批量移出购物车
    removeMany() {
      let list = []
      this.cartList.forEach(item => {
        if (item.selected) {
          list.push(item)
        }
      })
      if (list.length === 0) {
        Message({
          message: '请勾选需要移除的商品',
          type: 'error',
          showClose: true,
          duration: 3000
        })
        return
      }

      for (let i = 0; i < list.length; i++) {
        let index = this.cartList.indexOf(list[i])
        this.cartList.splice(index, 1)
      }

      Message({
        message: '移除成功',
        type: 'success',
        showClose: true,
        duration: 3000
      })
    },

    // 单个支付
    buyOne(id) {
      Message({
        message: '买了' + id,
        type: 'error',
        showClose: true,
        duration: 3000
      })
    },

    // 批量支付
    buyMany() {

    }

  }
}

</script>
<p>阐述下这个模块发开过程
内容:这个模块是一个购物车模块,主要实现了以下功能:</p>
<ol>
<li>获取购物车商品列表</li>
<li>商品数量修改</li>
<li>全选/单选商品</li>
<li>移除单个/批量商品</li>
<li>单个/批量购买商品</li>
</ol>
<p>在开发过程中,首先需要确定需要展示的内容和功能,然后进行页面的布局和组件的使用。在此模块中,使用了Element UI组件库中的Card、Row、Col、Checkbox、Button、InputNumber等组件,使页面更加美观和易用。</p>
<p>接着,需要编写数据和方法,其中数据包括购物车商品列表、全选状态和最小数量等,方法包括获取购物车列表、数量修改、全选/单选商品、移除单个/批量商品、单个/批量购买商品等。</p>
<p>在编写方法时,需要考虑各种情况的处理,例如移除商品时需要判断是否勾选了需要移除的商品,购买商品时需要区分单个和批量购买等。</p>
<p>最后,需要对模块进行测试,确保各个功能正常运作。如果发现问题,需要进行排查和修复。</p>
购物车 - 在线购物 - 全选/单选 - 移除 - 购买

原文地址: https://www.cveoy.top/t/topic/oPCo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录