diff --git a/src/store/modules/shoppingcart/index.ts b/src/store/modules/shoppingcart/index.ts index 6771758..9030058 100644 --- a/src/store/modules/shoppingcart/index.ts +++ b/src/store/modules/shoppingcart/index.ts @@ -4,7 +4,7 @@ import { getCompanyId } from '@/utils'; const useShoppingCartStore = defineStore('shoppingCart', { state: (): { shoppingCartList: GoodsBean[] } => ({ - shoppingCartList: uni.getStorageSync(`shoppingCart_${getCompanyId()}`) as GoodsBean[] + shoppingCartList: uni.getStorageSync(`shoppingCart_${getCompanyId()}`) == '' ? [] : uni.getStorageSync(`shoppingCart_${getCompanyId()}`) as GoodsBean[] }), persist: { @@ -16,7 +16,7 @@ const useShoppingCartStore = defineStore('shoppingCart', { uni.setStorageSync(`shoppingCart_${getCompanyId()}`, JSON.parse(value).shoppingCartList); }, getItem(key) { - return uni.getStorageSync(uni.getStorageSync(`shoppingCart_${getCompanyId()}`)); // [!code warning] + return uni.getStorageSync(uni.getStorageSync(`shoppingCart_${getCompanyId()}`)) || []; // [!code warning] } }, // 部分持久化状态的点符号路径数组,[]意味着没有状态被持久化(默认为undefined,持久化整个状态) @@ -33,7 +33,7 @@ const useShoppingCartStore = defineStore('shoppingCart', { }, getSameGoodsIndex: (state) => (goodsId: string, colorId: string, sizeId: string) => { - return state.shoppingCartList.findIndex(res => res.id === goodsId && res.checkedStock.colorId === colorId && res.checkedStock.sizeId === sizeId); + return state.shoppingCartList.findIndex(res => res.id === goodsId && res.checkedStock.colorId === colorId && res.checkedStock.sizeId === sizeId) || -1; } },