feat(store): 添加tabbar模块并导出
新增tabbar状态管理模块,包含tabbar项的状态、getters和actions 在store统一导出文件中添加tabbar模块的导出
This commit is contained in:
parent
cfced2e6d8
commit
e123a5cb1b
@ -15,3 +15,4 @@ export default store
|
|||||||
|
|
||||||
// 模块统一导出
|
// 模块统一导出
|
||||||
export * from './user'
|
export * from './user'
|
||||||
|
export * from './tabbar'
|
||||||
|
48
src/store/tabbar.ts
Normal file
48
src/store/tabbar.ts
Normal file
@ -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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user