store.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { tabbarList } from './config'
  2. interface CustomTabBarItem {
  3. text: string
  4. pagePath: string
  5. iconType: 'uniUi' | 'uiLib' | 'unocss' | 'iconfont' | 'image' // 不建议用 image 模式,需要配置2张图
  6. icon: any // 其实是 string 类型,这里是为了避免 ts 报错 (tabbar/index.vue 里面 uni-icons 那行)
  7. activeIcon?: string // 只有在 image 模式下才需要,传递的是高亮的图片(PS: 不建议用 image 模式)
  8. badge?: number | 'dot' // badge 显示一个数字或 小红点(样式可以直接在 tabbar/index.vue 里面修改)
  9. isBulge?: boolean // 是否是中间的鼓包tabbarItem
  10. }
  11. // pagePath 是 nativeTabbarList 和 customTabbarList 的关联点,如果没有对应上,会有问题!!
  12. // 如果希望通过接口调用 customTabbarList,可以在本文件里面调用接口
  13. export const customTabbarList: CustomTabBarItem[] = [
  14. {
  15. // text 和 pagePath 可以自己直接写,也可以通过索引从 nativeTabbarList 中获取
  16. text: '首页',
  17. pagePath: 'pages/index/index', // pagePath 是两者的关联点
  18. // 本框架内置了 uniapp 官方UI库 (uni-ui)的图标库
  19. // 使用方式如:<uni-icons type="home" size="30"/>
  20. // 图标列表地址:https://uniapp.dcloud.net.cn/component/uniui/uni-icons.html
  21. iconType: 'uniUi',
  22. icon: 'home',
  23. // badge: 'dot',
  24. },
  25. {
  26. text: tabbarList[1].text,
  27. pagePath: tabbarList[1].pagePath, // pagePath 是两者的关联点
  28. // 注意 unocss 图标需要如下处理:(二选一)
  29. // 1)在fg-tabbar.vue页面上引入一下并注释掉(见tabbar/index.vue代码第2行)
  30. // 2)配置到 unocss.config.ts 的 safelist 中
  31. iconType: 'unocss',
  32. icon: 'i-carbon-code',
  33. // badge: 10,
  34. },
  35. // {
  36. // pagePath: 'pages/mine/index',
  37. // text: '我的',
  38. // // 注意 iconfont 图标需要额外加上 'iconfont',如下
  39. // iconType: 'iconfont',
  40. // icon: 'iconfont icon-my',
  41. // },
  42. // {
  43. // pagePath: 'pages/index/index',
  44. // text: '首页',
  45. // // 使用 ‘image’时,需要配置 icon + iconActive 2张图片(不推荐)
  46. // // 既然已经用了自定义tabbar了,就不建议用图片了,所以不推荐
  47. // iconType: 'image',
  48. // icon: '/static/tabbar/home.png',
  49. // iconActive: '/static/tabbar/homeHL.png',
  50. // },
  51. ]
  52. /**
  53. * tabbar 状态,增加 storageSync 保证刷新浏览器时在正确的 tabbar 页面
  54. * 使用reactive简单状态,而不是 pinia 全局状态
  55. */
  56. export const tabbarStore = reactive({
  57. tabbarList: customTabbarList,
  58. curIdx: uni.getStorageSync('app-tabbar-index') || 0,
  59. prevIdx: uni.getStorageSync('app-tabbar-index') || 0,
  60. setCurIdx(idx: number) {
  61. this.curIdx = idx
  62. uni.setStorageSync('app-tabbar-index', idx)
  63. },
  64. setAutoCurIdx(path: string) {
  65. const index = tabbarList.findIndex(item => item.pagePath === path)
  66. // console.log('index:', index, path)
  67. // console.log('tabbarList:', tabbarList)
  68. if (index !== -1) {
  69. this.setCurIdx(index)
  70. }
  71. else {
  72. this.setCurIdx(0)
  73. }
  74. },
  75. restorePrevIdx() {
  76. if (this.prevIdx === this.curIdx)
  77. return
  78. this.setCurIdx(this.prevIdx)
  79. this.prevIdx = uni.getStorageSync('app-tabbar-index') || 0
  80. },
  81. })