基于vue2的简易购物车

包含功能:

全选;删除;统计总价;统计总数等

javascript 复制代码
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>

    <script>
      const app = new Vue({
        el: "#app",
        data: {
          // 水果列表
          fruitList: [
            {
              id: 1,
              icon: "./img/火龙果.png",
              isChecked: true,
              num: 2,
              price: 6,
            },
            {
              id: 2,
              icon: "./img/荔枝.png",
              isChecked: false,
              num: 7,
              price: 20,
            },
            {
              id: 3,
              icon: "./img/榴莲.png",
              isChecked: false,
              num: 3,
              price: 40,
            },
            {
              id: 4,
              icon: "./img/鸭梨.png",
              isChecked: true,
              num: 10,
              price: 3,
            },
            {
              id: 5,
              icon: "./img/樱桃.png",
              isChecked: false,
              num: 20,
              price: 34,
            },
          ],
        },
        watch: {
          fruitList:{
            deep:true,
            handler(newVal){
              // 我不想存本地就没写完这个功能
              // localStorage.setItem('list',JSON.stringify(newVal))
            }
          }
        },
        computed: {
          // 全选
          isAll:{
            get(){
              return this.fruitList.every((e) => !!e.isChecked);
            },
            set(val){
              this.fruitList.map((m) => (m.isChecked = val));
            }
           
          },
          // 统计总价
          totalPrice() {
            return this.fruitList.reduce((price, item) => {
              return item.isChecked ? price + item.price * item.num : price;
            }, 0);
          },
          // 统计总数
          totalCount() {
            return this.fruitList.reduce((sum, item) => {
              return item.isChecked ? sum + item.num : sum;
            }, 0);
          },
        },

        methods: {
          // 个数-
          decrease(id) {
            // 根据id找到对应数组项 -> find
            const fruit = this.fruitList.find((f) => f.id === id);
            fruit.num > 0 && fruit.num--;
          },
          // 个数+
          increase(id) {
            const fruit = this.fruitList.find((f) => f.id === id);
            fruit.num++;
          },
          // 删除
          del(id) {
            this.fruitList = this.fruitList.filter((f) => f.id !== id);
          },
        },
      });
    </script>
相关推荐
走粥2 小时前
使用indexOf查找对象结合Pinia持久化引发的问题
开发语言·前端·javascript
北寻北爱2 小时前
前端加密解密- base64、md5、sha256、AES
前端·vue.js
柒.梧.2 小时前
Redis通用命令+五大核心数据结构
前端·bootstrap·html
csbysj20202 小时前
Python break 语句详解
开发语言
Refly2 小时前
【微信接入 OpenClaw 龙虾🦞】10分钟手把手教程完成接入,Claude 模型无限使用
前端·微信·github
2401_857918292 小时前
C++中的访问者模式实战
开发语言·c++·算法
格林威2 小时前
工业相机图像高速存储(C++版):RAID 0 NVMe SSD 阵列暴力提速,附海康实战代码!
开发语言·c++·人工智能·数码相机·计算机视觉·工业相机·堡盟相机
恋猫de小郭2 小时前
为什么中转渠道的顶级模型会不好用?这是一个技术问题
前端·人工智能·ai编程
elseif1232 小时前
CSP-S提高级大纲
开发语言·数据结构·c++·笔记·算法·大纲·考纲