vue3+ts+vant4 实现购物车 前端代码

一、功能效果

二、前端代码

购物车的vue代码

<template>
  <van-nav-bar left-text="返回" title="购物车" @click-left="onClickLeft">

    <template #right>
      <van-popover v-model:show="showPopover" placement="bottom-end" :actions="actions" @select="onSelect">
        <template #reference>
          <van-icon name="more-o" />
        </template>
      </van-popover>
    </template>


  </van-nav-bar>

  <van-checkbox-group v-model="selectProducts" @change="ckGropChange">

    <van-swipe-cell v-for="(item, index) in cartItems" :key="item.id">

      <van-row class="box">
        <van-col span="2" class="ck"> <van-checkbox :name="item"></van-checkbox></van-col>
        <van-col span="22">
          <van-card :price="item.price" :desc="item.subName" :title="item.name" :thumb="item.img">
            <template #num>
              <van-stepper v-model="item.qty" theme="round" button-size="22" @change="computeTotalPrice" />
            </template>

          </van-card>


        </van-col>

      </van-row>


      <template #right>
        <van-button square text="删除" type="danger" class="delete-button" @click="del(item, index)" />
      </template>
    </van-swipe-cell>

  </van-checkbox-group>

  <van-submit-bar :price="totalPrice" button-text="提交订单" @submit="onSubmit">
    <van-checkbox v-model="checkedAll">全选</van-checkbox>
  </van-submit-bar>
</template>
<script setup lang="ts">
import { ref, onMounted, reactive, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { productApi } from '@/api/index'
const selectProducts: any = ref([])
const totalPrice = ref(0)
const showPopover = ref(false)
const checkedAll = ref(false)
const cartItems = ref([
  {
    id: 1,
    productId: 108,
    img: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
    name: '胜者心法:资治通鉴成事之道',
    subName: '胜者心法:这是子标题',
    price: "2.00",
    qty: 1,
    checked: true
  },
  {
    id: 2,
    productId: 109,
    img: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
    name: '认知破局 怎样突破能力、视野和思维的局限',
    subName: '胜者心法:这是子标题',
    price: "3.00",
    qty: 2,
    checked: false
  }
])
const actions = [
  { text: '清空所有', id: 1 },
  { text: '清空所选', id: 2 },
  { text: '选项三' },
];
onMounted(() => {
  selectProducts.value = cartItems.value.filter(item => item.checked == true)

  if (cartItems.value.length == selectProducts.value.length) {
    checkedAll.value = true
  }
  computeTotalPrice()
})

const onSelect = (action) => {
  if (action.id == 1) {
    //呼叫清空购物车清空接口
    cartItems.value = []
    selectProducts.value = []
  }

};
const ckGropChange = () => {
  computeTotalPrice()
  if (cartItems.value.length == selectProducts.value.length) {
    checkedAll.value = true
  }

  if (selectProducts.value.length == 0) {
    checkedAll.value = false
  }
}

const computeTotalPrice = () => {
  let total_Price = 0

  selectProducts.value.forEach((item: any) => {
    total_Price += item.qty * item.price
  });

  totalPrice.value = total_Price * 100
}

watch(() => checkedAll.value, (newValue, oldValue) => {
  if (newValue) {
    selectProducts.value = cartItems.value
  } else {

    selectProducts.value = []
  }

})
const del = (item: any, index: number) => {
  //呼叫接口,从用户购物车里面删除这个商品  item.productId
  cartItems.value.splice(index, 1)

  selectProducts.value = selectProducts.value.filter((it: any) => it.id != item.id)

  if (cartItems.value.length == selectProducts.value.length) {
    checkedAll.value = true
  }

  if (selectProducts.value.length == 0) {
    checkedAll.value = false
  }
}
const onSubmit = () => {

  console.log(selectProducts.value);
};



const onClickLeft = () => history.back();

</script>


<style scoped>
.delete-button {
  height: 100%;
}

.ck {
  display: flex;
  justify-content: center;
  align-items: center;
}

.box {
  border-radius: 15px;
  margin: 5px 10px 5px 10px;
  background-color: #fff;
}

.van-card {
  background-color: #fff;
  border-radius: 15px;
}
</style>

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天的接口写完了吗?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值