购物车 - 在线购物 - 全选/单选 - 移除 - 购买
<template>
<div class='container'>
<div class='cart-list'>
<pre><code> <div v-if='cartList.length < 1'>
<h1>购物车空空的,快去购物吧</h1>
</div>
<el-row v-if='cartList.length > 0'>
<el-col>
<el-card :body-style='{ padding: '0px' }'>
<div class='cart-list-header'>
<span style='font-size: 22px'>购物车(全部<span>{{ cartList.length }}</span>)</span>
<el-checkbox v-model='allCheck' class='all-check' @change='changeAllCheck'>全选</el-checkbox>
<el-button type='warning' @click='removeMany'>移除</el-button>
<el-button type='danger' @click='buyMany'>购买</el-button>
</div>
</el-card>
</el-col>
<el-col :span='24' v-for='(product,index) in cartList.slice().reverse()' :key='index'>
<el-card :body-style='{ padding: '0px' }'>
<div class='cart-item'>
<el-checkbox v-model='product.selected' class='checkedBox' @change='singleSelect'></el-checkbox>
<img :src='require('@/assets/goods/'+product.image)' class='product-image' alt=''>
<div class='product-title'>{{ product.title }}</div>
<div class='product-item-quantity'>
<span class='product-quantity-title'>数量:</span>
<el-input-number v-model='product.quantity' size='mini' :min='1' :max='product.stock' @change='quantityChange(product)' :step='1'></el-input-number>
</div>
<div class='product-item-size'>
<span class='product-quantity-title'>尺码:</span>
<span>{{product.size}}</span>
</div>
<div class='product-item-price'>
<span class='product-price-title'>单价:</span>
<span class='product-price'>¥{{ product.price }}</span>
</div>
<div class='product-item-price'>
<span class='product-price-title'>总价:</span>
<span class='product-price'>¥{{ product.amount }}</span>
</div>
<el-button type='warning' @click='removeOne(index)'>移除</el-button>
<el-button type='danger' @click='buyOne(product.id)'>购买</el-button>
</div>
</el-card>
</el-col>
</el-row>
</div>
</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 著作权归作者所有。请勿转载和采集!