在cart文件夹里新建一个子组件,childComps,新建CartList.vue。
我们在Cart.vue中一共要做三件事情
导航
商品列表
底部汇总
下面我们将商品列表单独封装一个组件CartList.vue,将这个组件在Cart中导入,注册,使用
<template>
<div class="cart-list">
<scroll class="content">
<cart-list-item v-for='(item,index) in cartList'
:item-info="item"
:key="index"></cart-list-item>
</scroll>
</div>
</template>
<script>
import {mapGetters} from 'vuex';
import Scroll from '@/components/common/scroll/Scroll';
import CartListItem from '@/views/cart/childComps/CartListItem';
export default {
name: 'CartList',
components:{
Scroll,
CartListItem,
},
computed:{
...mapGetters(['cartList'])
}
};
</script>
<style scoped>
.cart-list {
height: calc(100% - 44px - 49px - 40px);
}
.content {
height: 100%;
overflow: hidden;
}
</style>
然后cartListItem.vue如下:
<template>
<div id="shop-item">
<div class="item-selector">
</div>
<div class="item-img">
<img :src="itemInfo.image" alt="商品图片" />
</div>
<div class="item-info">
<div class="item-title">{{ itemInfo.title }}</div>
<div class="item-desc">商品描述: {{ itemInfo.desc }}</div>
<div class="info-bottom">
<div class="item-price left">¥{{ itemInfo.price }}</div>
<div class="item-count right">x{{ itemInfo.count }}</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'CartListItem',
components:{
},
props:{
itemInfo:{
type:Object,
default(){
return {}
}
}
},
methods:{
checkClick(){
this.itemInfo.checked=!this.itemInfo.checked;
}
}
};
</script>
<style scoped>
#shop-item {
width: 100%;
display: flex;
font-size: 0;
padding: 5px;
border-bottom: 1px solid #ccc;
}
.item-selector {
width: 14%;
display: flex;
justify-content: center;
align-items: center;
}
.item-title,
.item-desc {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.item-img {
padding: 5px;
/*border: 1px solid #ccc;*/
}
.item-img img {
width: 80px;
height: 100px;
display: block;
border-radius: 5px;
}
.item-info {
font-size: 17px;
color: #333;
padding: 5px 10px;
position: relative;
overflow: hidden;
}
.item-info .item-desc {
font-size: 14px;
color: #666;
margin-top: 15px;
}
.info-bottom {
margin-top: 10px;
position: absolute;
bottom: 10px;
left: 10px;
right: 10px;
}
.info-bottom .item-price {
color: orangered;
}
</style>
但是我们发现了购物车里面不能滚动,此时还是因为scrollHeight的原因,没有及时更新高度。此时我们在cartList中新建一个activated函数
activated() {
this.$refs.scroll.refresh();
}

购物车前面有一个按钮,我们对其做了一个封装
<template>
<div class="check-button" :class="{ check: isChecked }" >
<img src="~assets/img/cart/tick.svg" alt="" />
</div>
</template>
<script>
export default {
name: 'CheckButton',
props:{
isChecked:{
type:Boolean,
default:false
}
}
};
</script>
<style scoped>
.check-button {
border-radius: 50%;
border: 2px solid #aaaaaa;
}
.check {
border-color: #fb7299;
background-color: #fb7299;
}
</style>
然后将这个封装的组件导入到CartListItem里面,注册,使用。具体效果从上图中可以看出。

这个item选中不选中都是在模型中记录的.在模型中记录就意味着,我们之前有个cartList数组,数组里面有很多产品,比如商品1,商品2。我们需要在商品中添加一个叫做checked的属性,记录i这个商品到底选中不选中,默认情况下是选中状态。当我把商品添加到购物车的时候,默认就把这个商品在购物车里面处于选中状态。一旦用户点击了前面的按钮,就取消这个选中。模型发生改变界面才发生改变。所以我们需要在vuex里面添加商品时增加一个属性。
在mutations里面添加checked属性。
addCart(state,payload){
payload.checked=true
state.cartList.push(payload)
}
在checkButton里面接收了一个isChecked属性,它是由CartListItem中传进去的,所以我们在CartListItem中
<CheckButton :is-checked="itemInfo.checked"></CheckButton>
我们需要监听这个按钮的点击,监听组件的点击,要写上.native
<CheckButton :is-checked="itemInfo.checked" @click.native="checkClick"></CheckButton>
methods:{
checkClick(){
this.itemInfo.checked=!this.itemInfo.checked;
}
}
最终的效果如下:
