• 【Vue项目复习笔记】购物车商品列表展示


    在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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    然后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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101

    但是我们发现了购物车里面不能滚动,此时还是因为scrollHeight的原因,没有及时更新高度。此时我们在cartList中新建一个activated函数

     activated() {
        this.$refs.scroll.refresh();
      }
    
    • 1
    • 2
    • 3

    请添加图片描述

    购物车前面有一个按钮,我们对其做了一个封装

    <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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    然后将这个封装的组件导入到CartListItem里面,注册,使用。具体效果从上图中可以看出。

    下面我们来做一下购物车选中于不选中的状态

    在这里插入图片描述
    这个item选中不选中都是在模型中记录的.在模型中记录就意味着,我们之前有个cartList数组,数组里面有很多产品,比如商品1,商品2。我们需要在商品中添加一个叫做checked的属性,记录i这个商品到底选中不选中,默认情况下是选中状态。当我把商品添加到购物车的时候,默认就把这个商品在购物车里面处于选中状态。一旦用户点击了前面的按钮,就取消这个选中。模型发生改变界面才发生改变。所以我们需要在vuex里面添加商品时增加一个属性。
    在mutations里面添加checked属性。

      addCart(state,payload){
        payload.checked=true
        state.cartList.push(payload)
      }
    
    • 1
    • 2
    • 3
    • 4

    在checkButton里面接收了一个isChecked属性,它是由CartListItem中传进去的,所以我们在CartListItem中

      <CheckButton  :is-checked="itemInfo.checked"></CheckButton>
    
    • 1

    我们需要监听这个按钮的点击,监听组件的点击,要写上.native

     <CheckButton  :is-checked="itemInfo.checked" @click.native="checkClick"></CheckButton>
    
    • 1
      methods:{
        checkClick(){
          this.itemInfo.checked=!this.itemInfo.checked;
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    最终的效果如下:
    请添加图片描述

  • 相关阅读:
    带自动采集小说网站源码 小说听书网站源码 小说网站源码 带教程
    WiFi基础学习到实战(三:WiFi网络“物理层”)
    【每日刷题】Day72
    jenkins配置及实现接口自动化集成
    Taurus.MVC WebAPI 入门开发教程5:控制器安全校验属性【HttpGet、HttpPost】【Ack】【Token】【MicroService】。
    dockerfile基于apline将JDK20打包成镜像
    前端代码整洁思考
    Java 方法的使用
    以太坊历史发展进程
    验证的挑战
  • 原文地址:https://blog.csdn.net/qq_40992225/article/details/126275626