基于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>
相关推荐
徐小夕6 分钟前
花了一周,3亿tokens,我开源了一款 Word 文档智能审查平台,文稿自动质检+可视化分析,告别低效人工审核
前端·算法·github
Wang's Blog10 分钟前
Go-Zero 项目开发22:用户群聊功能的实现与完善
开发语言·golang
a11177630 分钟前
坦克大战3D Three.js 3D (开源项目)
开发语言·javascript·3d
kyriewen1 小时前
别再乱用useEffect了——你写的10个里有8个不该存在
前端·javascript·react.js
Ivanqhz2 小时前
Rust &‘static str浅析
java·前端·javascript·rust
IT_陈寒2 小时前
SpringBoot这个分页坑,我踩了三天才爬出来
前端·人工智能·后端
颜酱2 小时前
05 | 召回前置准备:根据业务数据库生成各数据库(读取配置阶段)
前端·人工智能·后端
Wang's Blog2 小时前
Go-Zero项目开发24: 基于Bitmap实现群聊消息已读未读
开发语言·后端·golang
zandy10113 小时前
衡石 Agentic BI的ReAct 推理框架在 Agentic BI 中的工程化实践
前端·javascript·react.js
罗超驿3 小时前
JavaEE进阶之路:从Web架构原理到HTML标签全解析
前端·html·web·javaee