store.ts 919 B

1234567891011121314151617181920212223242526272829303132
  1. import { tabbarList } from './config'
  2. /**
  3. * tabbar 状态,增加 storageSync 保证刷新浏览器时在正确的 tabbar 页面
  4. * 使用reactive简单状态,而不是 pinia 全局状态
  5. */
  6. export const tabbarStore = reactive({
  7. curIdx: uni.getStorageSync('app-tabbar-index') || 0,
  8. prevIdx: uni.getStorageSync('app-tabbar-index') || 0,
  9. setCurIdx(idx: number) {
  10. this.curIdx = idx
  11. uni.setStorageSync('app-tabbar-index', idx)
  12. },
  13. setAutoCurIdx(path: string) {
  14. const index = tabbarList.findIndex(item => item.pagePath === path)
  15. // console.log('index:', index, path)
  16. // console.log('tabbarList:', tabbarList)
  17. if (index !== -1) {
  18. this.setCurIdx(index)
  19. }
  20. else {
  21. this.setCurIdx(0)
  22. }
  23. },
  24. restorePrevIdx() {
  25. if (this.prevIdx === this.curIdx)
  26. return
  27. this.setCurIdx(this.prevIdx)
  28. this.prevIdx = uni.getStorageSync('app-tabbar-index') || 0
  29. },
  30. })