基于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>
相关推荐
Penge6662 分钟前
《Go 后端开发者学 TypeScript:从零搭建与运行》
javascript
Penge6661 小时前
《Go 后端开发者学 TypeScript:keyof关键字》
javascript
Penge6661 小时前
《Go 后端开发者学 TypeScript:extends关键字》
javascript
张张123y2 小时前
DSH 插件调试避坑:client.js 设置面板、热重载与高频报错排查表
开发语言·javascript·人工智能·开源·ecmascript·后端开发·ai agent
去伪存真2 小时前
AI Agent Harness 深度测评与技术选型实战
前端·agent
冻柠檬飞冰走茶4 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
FRing035 小时前
vue2干货总结7(自定义指令、插槽、单页应用程序、路由的使用(VueRouter))
开发语言·前端·vue.js·vue2
雪落漂泊5 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
波力海苔夹心脆6756 小时前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
不会算法的笨小孩6 小时前
异常的使用以及会出现的问题
开发语言·c++·c++11·异常