useTabbar.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import icon3 from '@/static/tab/cart1.png'
  2. import icon4 from '@/static/tab/cart2.png'
  3. import icon5 from '@/static/tab/my1.png'
  4. import icon6 from '@/static/tab/my2.png'
  5. import class1 from '@/static/tab/class-tab0.png'
  6. import class2 from '@/static/tab/class-tab1.png'
  7. export interface TabbarItem {
  8. name: string
  9. value: number | null
  10. active: boolean
  11. title: string
  12. icon2: string
  13. icon1: string
  14. }
  15. const tabbarItems = ref<TabbarItem[]>([
  16. { name: 'smqjh-home', value: null, active: true, title: '首页', icon1: '', icon2: '' },
  17. { name: 'smqjh-classfiy', value: null, active: false, title: '分类', icon1: class1, icon2: class2 },
  18. { name: 'smqjh-cart', value: null, active: false, title: '购物车', icon1: icon3, icon2: icon4 },
  19. { name: 'smqjh-my', value: null, active: false, title: '我的', icon1: icon5, icon2: icon6 },
  20. ])
  21. export function useTabbar() {
  22. const tabbarList = computed(() => tabbarItems.value)
  23. const activeTabbar = computed(() => {
  24. const item = tabbarItems.value.find(item => item.active)
  25. return item || tabbarItems.value[0]
  26. })
  27. const getTabbarItemValue = (name: string) => {
  28. const item = tabbarItems.value.find(item => item.name === name)
  29. return item && item.value ? item.value : null
  30. }
  31. const setTabbarItem = (name: string, value: number) => {
  32. const tabbarItem = tabbarItems.value.find(item => item.name === name)
  33. if (tabbarItem) {
  34. tabbarItem.value = value
  35. }
  36. }
  37. const setTabbarItemActive = (name: string) => {
  38. tabbarItems.value.forEach((item) => {
  39. if (item.name === name) {
  40. item.active = true
  41. }
  42. else {
  43. item.active = false
  44. }
  45. })
  46. }
  47. return {
  48. tabbarList,
  49. activeTabbar,
  50. getTabbarItemValue,
  51. setTabbarItem,
  52. setTabbarItemActive,
  53. }
  54. }