useTabbar.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. export interface TabbarItem {
  2. name: string
  3. value: number | null
  4. active: boolean
  5. title: string
  6. icon: string
  7. }
  8. const tabbarItems = ref<TabbarItem[]>([
  9. { name: 'home', value: null, active: true, title: '首页', icon: 'home' },
  10. { name: 'my', value: null, active: false, title: '我的', icon: 'user' },
  11. ])
  12. export function useTabbar() {
  13. const tabbarList = computed(() => tabbarItems.value)
  14. const activeTabbar = computed(() => {
  15. const item = tabbarItems.value.find(item => item.active)
  16. return item || tabbarItems.value[0]
  17. })
  18. const getTabbarItemValue = (name: string) => {
  19. const item = tabbarItems.value.find(item => item.name === name)
  20. return item && item.value ? item.value : null
  21. }
  22. const setTabbarItem = (name: string, value: number) => {
  23. const tabbarItem = tabbarItems.value.find(item => item.name === name)
  24. if (tabbarItem) {
  25. tabbarItem.value = value
  26. }
  27. }
  28. const setTabbarItemActive = (name: string) => {
  29. tabbarItems.value.forEach((item) => {
  30. if (item.name === name) {
  31. item.active = true
  32. }
  33. else {
  34. item.active = false
  35. }
  36. })
  37. }
  38. return {
  39. tabbarList,
  40. activeTabbar,
  41. getTabbarItemValue,
  42. setTabbarItem,
  43. setTabbarItemActive,
  44. }
  45. }