Vant购物车核心代码


Vant购物车核心代码

pinia

import { ref, computed } from "vue";
import { defineStore } from "pinia";

export const Info = defineStore("Info", () => {
  // 店铺信息
  let shopInfo = ref<any>({});
  let changeInfo = (val: any) => {
    shopInfo.value = val;
  };
  // 商品
  let goods = ref<any>([]);
  let GoodsChange = (val: any) => {
    goods.value = val;
  };
  // 商品数量增加减少
  let addNum = (val: any, num: any) => {
    for (let obj of goods.value) {
      for (let arr of obj.foods) {
        if (arr.id === val.id) {
          arr.num += num;
        }
      }
    }
  };
  // 清空购物车
  let clearCart = () => {
    for (let obj of goods.value) {
      for (let child of obj.foods) {
        child.num = 0;
      }
    }
  };
  // 计算商品大于0
  let shopPing = computed(() => {
    let newArr = [];
    for (let obj of goods.value) {
      for (let arr of obj.foods) {
        if (arr.num > 0) {
          newArr.push(arr);
        }
      }
    }
    return newArr;
  });
  // 商品详情
  let goodsInfo = ref<any>({});
  let changeGoods = (val: any) => {
    goodsInfo.value = val;
  };
  return {
    shopInfo,
    changeInfo,
    goodsInfo,
    changeGoods,
    goods,
    GoodsChange,
    addNum,
    shopPing,
    clearCart,
  };
});


扫描二维码,在手机上阅读

推荐阅读:

vant左右联动核心代码

Vue3+vite项目内网访问

评 论