Przeglądaj źródła

fix(tabbar): 修复直接进入非首页时tabbar索引不正确的问题

添加 setAutoCurIdx 方法根据当前路径自动设置正确的 tabbar 索引
feige996 8 miesięcy temu
rodzic
commit
6f3d99402c
2 zmienionych plików z 15 dodań i 1 usunięć
  1. 3 0
      src/App.vue
  2. 12 1
      src/tabbar/store.ts

+ 3 - 0
src/App.vue

@@ -1,6 +1,7 @@
 <script setup lang="ts">
 import { onHide, onLaunch, onShow } from '@dcloudio/uni-app'
 import { navigateToInterceptor } from '@/router/interceptor'
+import { tabbarStore } from './tabbar/store'
 import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
 
 onLaunch((options) => {
@@ -13,6 +14,8 @@ onLaunch((options) => {
   else {
     navigateToInterceptor.invoke({ url: '/' })
   }
+  // 处理直接进入路由非首页时,tabbarIndex 不正确的问题
+  tabbarStore.setAutoCurIdx(options.path)
 })
 onShow((options) => {
   console.log('App Show', options)

+ 12 - 1
src/tabbar/store.ts

@@ -1,3 +1,4 @@
+import { tabbarList } from './config'
 /**
  * tabbar 状态,增加 storageSync 保证刷新浏览器时在正确的 tabbar 页面
  * 使用reactive简单状态,而不是 pinia 全局状态
@@ -9,7 +10,17 @@ export const tabbarStore = reactive({
     this.curIdx = idx
     uni.setStorageSync('app-tabbar-index', idx)
   },
-
+  setAutoCurIdx(path: string) {
+    const index = tabbarList.findIndex(item => item.pagePath === path)
+    // console.log('index:', index, path)
+    // console.log('tabbarList:', tabbarList)
+    if (index !== -1) {
+      this.setCurIdx(index)
+    }
+    else {
+      this.setCurIdx(0)
+    }
+  },
   restorePrevIdx() {
     if (this.prevIdx === this.curIdx)
       return