基于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>
相关推荐
乘风gg27 分钟前
还在养虾吗?虾王已诞生:微信龙虾 ClawBot
前端·ai编程·claude
小小小小宇43 分钟前
LLM 长期记忆构建
前端
lichenyang4531 小时前
从 Express 老项目到 NestJS + Docker:一次车辆管理系统的渐进式重构
前端
竹林8182 小时前
用 wagmi v2 + viem 监听链上事件,我踩了三天坑终于搞懂了实时日志与历史补全
javascript
Momo__2 小时前
VueUse createReusableTemplate —— 单文件组件内的模板复用神器
前端·vue.js
只一2 小时前
😭从回调地狱到 async/await:一文打通 Ajax 与 JS 异步编程
javascript
程序员小富2 小时前
我开源了一个开发者专属的智能 JSON 工具,得到了媳妇高度认可
前端·vue.js·后端
小小小小宇2 小时前
程序员如何给 LLM 装工具以及看懂推理过程
前端
写代码的皮筏艇2 小时前
React中的forwardRef
前端·react.js·面试
槑有老呆2 小时前
花三个月工资请了个 AI 程序员,结果它连青岛啤酒股价都查不了
前端