关于购物车控制隐藏逻辑的问题

来源:1-5 项目作业

API调用工程师

2021-07-27 00:22:17

相关截图:

http://img.mukewang.com/climg/60fedfd009754ed403900608.jpg

问题描述:

当我第一个共计的时候,两个都会展开显示。

尝试过的解决方式:

我尝试过给每个shop中传一个参数showAll去控制但是不起作用,可能是应为计算属性的返回的ref对象里的数据不具备响应式 

老师 这种情况应该怎么解决

相关代码:

​<template>
<div class="wrapper">
<div class="title">我的全部购物车({{cartListWithProducts.length}})</div>
<div class="shop_wrapper">
<div class="shops">
<div
class="empty"
v-if="cartListWithProducts.length === 0"
>购物车当前为空</div>
<div
class="shop"
v-for="(item, index) in cartListWithProducts"
:key="index"
>
<div class="shop_title">
{{item.shopName}}
</div>
<div class="products">
<div class="products_list">
<template
v-for="(product,index) in item.productList"
:key="product._id"
>
<div
class="products_item"
v-if='showAll ? index>=0 : index<2'
>
<img class="products_item_img" :src="product.imgUrl" />
<div class="products_item_detail">
<h4 class="products_item_title">{{product.name}}</h4>
<p class="products_item_price">
<span>
<span class="products_item_yen">&yen; </span>
{{product.price}} x {{product.count}}
</span>
<span class="products_item_total">
<span class="products_item_yen">&yen; </span>
{{(product.price * product.count).toFixed(2)}}
</span>
</p>
</div>
</div>
</template>
<div class="products_list_btn" v-if='item.productList.length >0' @click='showAllProduct'>
共计:{{item.productList.length}}件/1.4kg
<span class="product_list_btn_icon iconfont">&#xe60b;</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<Docker :currentIndex="1"/>
</template>

<script>
import { useStore } from 'vuex'
import { computed, ref } from 'vue'
import Docker from '../../components/Docker'

const useCart = () => {
const store = useStore()
const cartList = JSON.parse(JSON.stringify(store.state.cartList))
const cartListWithProducts = computed(() => {
const newCartList = []
for (const shopId in cartList) {
const shopInfo = cartList[shopId]
const productListArr = []
let total = 0
const products = cartList[shopId]?.productList
for (const productId in products) {
const product = products[productId]
if (product.count > 0) {
productListArr.push(products[productId])
}
total += product.count || 0
}
if (total > 0) {
// 出错点!!!
shopInfo.productList = productListArr
newCartList.push(shopInfo)
}
}
return newCartList
})
const showAll = ref(false)
const showAllProduct = () => {
showAll.value = !showAll.value
}
return { cartListWithProducts, showAllProduct, showAll }
}
export default {
name: 'CartList',
components: { Docker },
setup () {
const { cartListWithProducts, showAllProduct, showAll } = useCart()
return { cartListWithProducts, showAllProduct, showAll }
}
}
</script>

<style scoped>
@import '../../style/viriables.scss';
@import '../../style/mixin.scss';
.wrapper {
position: absolute;
left: 0;
top: 0;
bottom: .5rem;
right: 0;
background: $dark-bgColor;
}
.title {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1;
line-height: .44rem;
background: $bg-color;
font-size: .16rem;
color: $content-fontcolor;
text-align: center;
}
.empty {
line-height: .44rem;
color: $light-fontColor;
font-size: .16rem;
text-align: center;
}
.shop_wrapper{
overflow-y: scroll;
position: absolute;
top: .44rem;
right: .18rem;
bottom: 0;
left: .18rem;
}
.shop {
margin-bottom: .16rem;
background-color: $bg-color;
&_title {
padding: .16rem;
font-size: .16rem;
color: $content-fontcolor;
}
}
.products {
&_list {
background: $bg-color;
&_btn {
background: #F5F5F5;
font-size: .14rem;
color: #999999;
text-align: center;
height: .28rem;
line-height: .28rem;
}
}
&_item {
position: relative;
display: flex;
padding: 0 .16rem 0.16rem .16rem;
&_img {
width: .46rem;
height: .46rem;
margin-right: .16rem;
}
&_detail {
flex: 1;
}
&_title {
margin: 0;
line-height: .2rem;
font-size: .14rem;
color: $content-fontcolor;
@include singleline-ellipsis;
}
&_price {
display: flex;
margin: .06rem 0 0 0;
line-height: .2rem;
font-size: .14rem;
color: $hightlight-fontColor;
}
&_total {
flex: 1;
text-align: right;
color: $dark-fontColor;
}
&_yen {
font-size: .12rem;
}
}
}
</style>


写回答

1回答

好帮手慕慕子

2021-07-27

同学你好,可以结合注释测试理解下面的代码

<template>
<div class="wrapper">
<div class="title">我的全部购物车({{cartListWithProducts.length}})</div>
<div class="shop_wrapper">
<div class="shops">
<div class="empty" v-if="cartListWithProducts.length === 0">购物车当前为空</div>
<div class="shop" v-for="(item, index) in cartListWithProducts" :key="index" >
<div class="shop_title"> {{item.shopName}} </div>
<div class="products">
<div class="products_list">
<!-- 遍历新添加的属性newCartList -->
<!-- <template v-for="(product,index) in item.productList" :key="product._id" > -->
<template v-for="(product,index) in item.newCartList" :key="product._id" >
<!-- 通过索引与showProductCount比较,控制需要被隐藏的商品 -->
<!-- <div class="products_item" v-if='showAll ? index>=0 : index<2'> -->
<div class="products_item" v-show="index < item.showProductCount">
<img class="products_item_img" :src="product.imgUrl" />
<div class="products_item_detail">
<h4 class="products_item_title">{{product.name}}</h4>
<p class="products_item_price">
<span>
<span class="products_item_yen">&yen; </span>
{{product.price}} x {{product.count}}
</span>
<span class="products_item_total">
<span class="products_item_yen">&yen; </span>
{{(product.price * product.count).toFixed(2)}}
</span>
</p>
</div>
</div>
</template>
<!-- 点击事件传入item,方便控制当前被点击的商品信息,调整为商品数大于等于3时显示该按钮 -->
<!-- <div class="products_list_btn" v-if='item.productList.length >0' @click='showAllProduct'> -->
<div class="products_list_btn" v-if='item.newCartList.length >= 3' @click='showAllProduct(item)'>

<!-- 共计:{{item.productList.length}}件/1.4kg -->
共计:{{item.newCartList.length}}件/1.4kg

<span class="product_list_btn_icon iconfont">&#xe60b;</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<Docker :currentIndex="1"/>
</template>

<script>
import { useStore } from 'vuex';
import { computed, ref } from 'vue';
import Docker from '../home/Docker';

const useCart = () => {
const store = useStore();
// 从store中获取cartList数据
const {cartList} = store.state
// const cartList = JSON.parse(JSON.stringify(store.state.cartList));

const cartListWithProducts = computed(() => {
// const newCartList = [];
for (const shopId in cartList) {
const shopInfo = cartList[shopId];
// 添加一个新属性,以数组的形式存储商品信息
shopInfo.newCartList = []
// 添加一个新属性,用来表示最多只能显示多少条商品,默认只显示两条
shopInfo.showProductCount = 2
// const productListArr = [];
// let total = 0;
const products = cartList[shopId]?.productList;
for (const productId in products) {
// 将数据添加到数组中
shopInfo.newCartList.push(products[productId])
// const product = products[productId];
// if (product.count > 0) {
// productListArr.push(products[productId]);
// }
// total += product.count || 0;
}
// if (total > 0) {
// // 出错点!!!
// shopInfo.productList = productListArr;
// newCartList.push(shopInfo);
// }
}
// return newCartList;
// 直接返回调整后cartList即可
return cartList
});
const showAll = ref(false);
// const showAllProduct = () => {
// showAll.value = !showAll.value;
// };
// 调整点击事件函数逻辑
const showAllProduct = (item) => {
item.showProductCount === item.newCartList.length ? item.showProductCount = 2 : item.showProductCount = item.newCartList.length
};
return { cartListWithProducts, showAllProduct, showAll };
};
export default {
name: 'CartList',
components: { Docker },
setup() {
const { cartListWithProducts, showAllProduct, showAll } = useCart();
return { cartListWithProducts, showAllProduct, showAll };
},
};
</script>

​另外,这是一个项目作业,如果还有问题,建议同学可以暂时先完成其他功能,然后提交作业,批作业的老师会针对你的完整的项目进行测试与调整,并将修改建议整理成文档发送给同学,便于同学查看与修改。

祝学习愉快~

1

0 学习 · 15276 问题

查看课程