购物车页面

购物车页面

老师,购物车页面的js逻辑怎么写?我写的有问题

<template>
  <div class="wrapper">
    <div class="title">我的全部购物车(2)</div>
    <div class="title__content">
      <template v-for="(productItem, index) in newCartList" :key="index">
        <div class="product" v-if="productItem.productList">
          <div class="product__title">{{ productItem.shopName }}</div>
          <div class="product__list">
            <div
              v-for="item in productItem.productList"
              :key="item._id"
              class="content__product__item"
            >
              <img :src="item.imgUrl" alt="" />
              <div class="content__product__con">
                <p>{{ item.name }}</p>
                <p>
                  <span class="content__product__con__price"
                    >&yen;{{ item.price }}X{{ item.count }}</span
                  >
                  <span>&yen;{{ (item.price * item.count).toFixed(2) }}</span>
                </p>
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>
  <Docker :currentIndex="1" />
</template>
<script>
import Docker from "../../components/Docker.vue";
import { useStore } from "vuex";
import { reactive } from 'vue';
export default {
  name: "CartList",
  components: {
    Docker,
  },
  setup() {
    const store = useStore();
    const cartList = store.state.cartList;
    const newCartList = reactive({});
    for (const shopId in cartList) {
      const shopInfo = cartList[shopId];
      for (const item in shopInfo.productList) {
        const product = shopInfo.productList[item];
        if (product.count > 0) {
          newCartList[shopId] = shopInfo;
          shopInfo.productList[item] = product;
        }
      }
    }
    console.log(newCartList);
    return { newCartList };
  },
};
</script>
<style lang="scss" scoped>
@import "../../style/variables.scss";
.wrapper {
  overflow-y: auto;
  position: absolute;
  right: 0;
  top: 0;
  left: 0;
  bottom: 0.5rem;
  background: #f8f8f8;
}
.title {
  height: 0.44rem;
  background: #fff;
  line-height: 0.44rem;
  text-align: center;
  font-size: 16px;
  color: #333333;
}
.product {
  overflow-y: auto;
  margin: 0.16rem 0.18rem 0;
  box-sizing: border-box;
  border-radius: 4px;
  background: $color-white;
  height: 2.22rem;
  &__title {
    padding: 0.16rem 0.16rem 0;
    background: $color-white;
    font-size: 16px;
    color: #140f0f;
    font-weight: bold;
  }
  &__list {
    background: $color-white;
    padding-bottom: 0.16rem;
    .content__product__item {
      padding: 0.16rem 0.16rem 0;
      position: relative;
      display: flex;
      img {
        width: 0.46rem;
        height: 0.46rem;
        margin-right: 0.16rem;
      }
    }
    .content__product__con {
      flex: 1;
      p {
        margin: 0;
      }
      p:first-of-type {
        font-size: 14px;
        color: $content-fontcolor;
      }
      p:last-of-type {
        margin-top: 0.06rem;
        span:last-of-type {
          float: right;
        }
      }
      &__price {
        color: $color-red;
      }
    }
  }
}
</style>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小李 2023-08-03 18:42:58

同学你好,这里做个判断在count为0时将其整体delete删掉。将count不为0的重新赋给newcartList即可。

祝学习愉快!

  • 提问者 慕芸芸 #1

    问题已解决,谢谢老师

    2023-08-03 20:37:49
  • 提问者 慕芸芸 #2

    李哥,我还有个问题

    https://img1.sycdn.imooc.com//climg/64cc3d77090f9d8705940574.jpg

    这个newCartList声明的时候就是个普通对象,输出的时候为啥是个响应式的呢?

    https://img1.sycdn.imooc.com//climg/64cc3db509fe334d02290043.jpg

    2023-08-04 07:53:04
  • 好帮手慕小李 回复 提问者 慕芸芸 #3

    newCartList就是个普通对象。

    2023-08-04 09:55:48
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师