From e123a5cb1b909264b7bcfea657c6b79942ad54b9 Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Mon, 26 May 2025 23:43:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(store):=20=E6=B7=BB=E5=8A=A0tabbar?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=B9=B6=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增tabbar状态管理模块,包含tabbar项的状态、getters和actions 在store统一导出文件中添加tabbar模块的导出 --- src/store/index.ts | 1 + src/store/tabbar.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/store/tabbar.ts diff --git a/src/store/index.ts b/src/store/index.ts index 74b1b2f..249f53f 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -15,3 +15,4 @@ export default store // 模块统一导出 export * from './user' +export * from './tabbar' diff --git a/src/store/tabbar.ts b/src/store/tabbar.ts new file mode 100644 index 0000000..4216cc5 --- /dev/null +++ b/src/store/tabbar.ts @@ -0,0 +1,48 @@ +import { defineStore } from 'pinia' + +export interface TabbarItem { + name: string + value: number | null + active: boolean +} + +export const useTabbarStore = defineStore('tabbar', { + state: (): { tabbarItems: TabbarItem[] } => ({ + tabbarItems: [ + { name: 'index', value: null, active: true }, + { name: 'about', value: null, active: false }, + ], + }), + getters: { + getTabbarItems: (state) => { + return state.tabbarItems + }, + getActive: (state) => { + const item = state.tabbarItems.find((item) => item.active) + return item || state.tabbarItems[0] + }, + getTabbarItemValue: (state) => { + return (name: string) => { + const item = state.tabbarItems.find((item) => item.name === name) + return item && item.value ? item.value : null + } + }, + }, + actions: { + setTabbarItem(name: string, value: number) { + const tabbarItem = this.tabbarItems.find((item) => item.name === name) + if (tabbarItem) { + tabbarItem.value = value + } + }, + setTabbarItemActive(name: string) { + this.tabbarItems.forEach((item) => { + if (item.name === name) { + item.active = true + } else { + item.active = false + } + }) + }, + }, +})