Browse Source

refactor(购物车): 优化购物车数量显示逻辑

移除通过props传递的cartCount,统一使用store计算购物车数量
修复导航栏和底部栏的样式问题
master
wei 3 days ago
parent
commit
16fd0ab11c
  1. 25
      components/custom-tab-bar/my-tab-bar.vue
  2. 2
      pages/home/home.vue
  3. 6
      pages/recipeOrder/recipeOrder.vue
  4. 2
      pages/shoppingCart/shoppingCart.vue

25
components/custom-tab-bar/my-tab-bar.vue

@ -1,15 +1,15 @@
<script setup>
import { onReady } from "@dcloudio/uni-app";
import { computed, getCurrentInstance, ref, watch } from "vue";
import { computed, getCurrentInstance, ref } from "vue";
import { useRect } from "@/libs/utils";
import useStore from "@/store";
const props = defineProps({
tabIndex: String | Number,
cartCount: {
default: 0,
type: Number,
},
// cartCount: {
// default: 0,
// type: Number,
// },
});
const store = useStore();
@ -30,13 +30,8 @@ const activeColor = "#06CA64";
/**
* 购物车数量
*/
// const cartCount = computed(() => {
// return store.cartList.reduce((acc, cur) => acc + cur.quantity, 0);
// });
const cartCount = ref(0);
watch(() => store.cartList, (value) => {
cartCount.value = value.reduce((acc, cur) => acc + cur.quantity, 0);
const cartCount = computed(() => {
return store.cartList.reduce((acc, cur) => acc + cur.quantity, 0);
});
/**
@ -121,10 +116,10 @@ onReady(() => {
</view>
<template v-if="index === 3">
<view v-if="props.cartCount !== 0" class="badge">
<!-- <view v-if="props.cartCount !== 0" class="badge">
{{ props.cartCount }}
</view>
<view v-else-if="cartCount !== 0" class="badge">
</view> -->
<view v-if="cartCount !== 0" class="badge">
{{ cartCount }}
</view>
</template>

2
pages/home/home.vue

@ -591,7 +591,7 @@ onShow(async () => {
<template>
<navv>
<template #default="{ status: statusBarHeight, menus: locationBarHeight }">
<template #default="{ status: statusBarHeight, menu: locationBarHeight }">
<view
class="main"
:style="{

6
pages/recipeOrder/recipeOrder.vue

@ -55,8 +55,6 @@ const totalPrice = ref(0);
const serviceFee = ref(0);
//
const shippingFee = ref(0);
//
const cartCount = ref(0);
// const defaultAddressId = ref("");
// const isSearch = ref(false);
// const showClear = ref(false);
@ -297,7 +295,7 @@ async function initCartInfo() {
// store.changeCartList([]);
// await sleep(500);
store.changeCartList(res.data);
cartCount.value = store.getCartTotalQuantity();
// cartCount.value = store.getCartTotalQuantity();
// store.cartList = res.data;
if (store.cartList.length > 0) {
preview(store.cartList.map(item => item.id));
@ -483,7 +481,7 @@ onShow(() => {
/>
</view>
<customTabBar tab-index="2" :cart-count="cartCount" />
<customTabBar tab-index="2" />
</template>
</navv>
</template>

2
pages/shoppingCart/shoppingCart.vue

@ -732,7 +732,7 @@ onLoad(() => {
<!-- 底部统计栏 -->
<view
class="under-box" :style="{
bottom: `${store.tabbarBottomHeight - 30}px`,
bottom: `${store.tabbarBottomHeight}px`,
}"
>
<view

Loading…
Cancel
Save