购物车逻辑
This commit is contained in:
@@ -1,20 +1,43 @@
|
||||
import { defineStore } from 'pinia';
|
||||
|
||||
const useShoppingCartStore = defineStore('shoppingCart', {
|
||||
state: () => ({
|
||||
list: [],
|
||||
total: 0,
|
||||
count: 0
|
||||
state: (): { shoppingCartList: any[] } => ({
|
||||
shoppingCartList: []
|
||||
}),
|
||||
|
||||
persist: {
|
||||
// 修改存储中使用的键名称,默认为当前 Store的 id
|
||||
key: 'shoppingCartState',
|
||||
storage: {
|
||||
setItem(key, value) {
|
||||
uni.setStorageSync(key, value); // [!code warning]
|
||||
},
|
||||
getItem(key) {
|
||||
return uni.getStorageSync(key); // [!code warning]
|
||||
}
|
||||
},
|
||||
// 部分持久化状态的点符号路径数组,[]意味着没有状态被持久化(默认为undefined,持久化整个状态)
|
||||
paths: undefined
|
||||
},
|
||||
|
||||
getters: {
|
||||
getList() {
|
||||
return this.list;
|
||||
getShoppingCartList(): any[] {
|
||||
return this.shoppingCartList;
|
||||
},
|
||||
getTotal() {
|
||||
return this.total;
|
||||
|
||||
getTotalCount(): number {
|
||||
return this.shoppingCartList.length;
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
save(partial: Partial<any>) {
|
||||
this.shoppingCartList.push(partial);
|
||||
},
|
||||
getCount() {
|
||||
return this.count;
|
||||
|
||||
clear() {
|
||||
this.shoppingCartList = [];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default useShoppingCartStore;
|
||||
|
Reference in New Issue
Block a user