Преглед изворни кода

feat: 调整返回的路由参数,防止报错;

liujia пре 1 месец
родитељ
комит
c6af37c
1 измењених фајлова са 12 додато и 47 уклоњено
  1. 12 47
      src/layout/index.vue

+ 12 - 47
src/layout/index.vue

@@ -13,14 +13,6 @@
         </div>
         <div class="text">雷能信息后台管理</div>
       </div>
-      <!-- <a-menu
-        v-model:selectedKeys="state.selectedKeys"
-        mode="inline"
-        :theme="theme"
-        :inline-collapsed="state.collapsed"
-        :items="menus"
-        @click="clickMenuItemHandler"
-      ></a-menu> -->
       <SideMenu :collapsed="collapsed" />
     </a-layout-sider>
 
@@ -62,7 +54,6 @@
         <slot name="footer">
           <span>合肥雷能信息技术有限公司 © 2025 All Rights Reserved.</span>
           <span>&nbsp;&nbsp;&nbsp;&nbsp;版本号:{{ version }}</span>
-          <!-- <span>&nbsp;&nbsp;&nbsp;&nbsp;构建时间:{{ buildTime }}</span> -->
         </slot>
       </a-layout-footer>
     </a-layout>
@@ -74,7 +65,6 @@
 <script setup lang="ts">
 import { ref, reactive, watchEffect, computed, h, onUnmounted, onMounted } from 'vue'
 import userDropdown from './components/userDropdown/index.vue'
-// import { menus } from '@/const/menus'
 import { useRoute, useRouter } from 'vue-router'
 import timeNow from './components/timeNow/index.vue'
 import type { Route } from 'ant-design-vue/es/breadcrumb/Breadcrumb'
@@ -87,7 +77,6 @@ import SideMenu from './components/sideMenu/index.vue'
 const userStore = useUserStore()
 const userId = ref(userStore?.userInfo?.userId || '')
 const version = __APP_VERSION__
-// const buildTime = __BUILD_TIME__
 
 const emit = defineEmits(['refresh'])
 // 刷新
@@ -103,23 +92,11 @@ const onBreakpoint = (broken: boolean) => {
   }
 }
 
-// 需要缓存的组件列表
-// const keepAliveComponents = computed(() => {
-//   return route.matched.filter((item) => item.meta.keepAlive).map((item) => item.name as string)
-// })
-
 let mqttClient: MqttClient | null = null
-// let mqttTimeout: number | null = null
-// const MQTT_TIMEOUT_MS = 10000 // 10秒
 
 const resetMqttTimeout = () => {
   console.log('🚀关闭MQTT连接')
   closeMqtt()
-  // if (mqttTimeout) clearTimeout(mqttTimeout)
-  // mqttTimeout = window.setTimeout(() => {
-  //   console.log('MQTT超时未收到新消息')
-  //   closeMqtt()
-  // }, MQTT_TIMEOUT_MS)
 }
 
 const MqttData = ref()
@@ -311,45 +288,33 @@ const itemRender = (options: { route: Route; params: any; routes: Route[]; paths
 
 const clickedMenuItem = ref('')
 watchEffect(() => {
-  // if (routes.value.length > 0 && routes.value[0].path) {
-  //   state.selectedKeys = [routes.value[0].path.split('/')[1] || 'home']
-  // }
   state.selectedKeys = [clickedMenuItem.value]
 })
 
-// const clickMenuItemHandler = ({ key, keyPath }: { key: string; keyPath: string[] }) => {
-//   console.log('🚀clickMenuItemHandler🚀', key, keyPath)
-//   clickedMenuItem.value = key
-//   if (route.name !== key) {
-//     router.push({ name: key })
-//   }
-// }
-
 const canGoBack = ref(false)
 const navigationHistory = ref<string[]>([])
 let isBackNavigation = false
 
 onMounted(() => {
-  // 初始化导航历史
-  navigationHistory.value = [router.currentRoute.value.path]
+  // 初始化记录当前完整路径(包含参数)
+  navigationHistory.value = [router.currentRoute.value.fullPath]
   canGoBack.value = false
 })
 
-// 监听路由变化,更新导航历史和canGoBack状态
 watchEffect(() => {
-  const currentPath = router.currentRoute.value.path
+  const currentFullPath = router.currentRoute.value.fullPath
 
   if (isBackNavigation) {
-    // 如果是返回导航,不添加到历史记录
+    // 返回导航时不记录
     isBackNavigation = false
   } else {
-    // 确保只添加新的路径
-    if (navigationHistory.value[navigationHistory.value.length - 1] !== currentPath) {
-      navigationHistory.value.push(currentPath)
+    // 只记录新的路径(避免重复)
+    if (navigationHistory.value[navigationHistory.value.length - 1] !== currentFullPath) {
+      navigationHistory.value.push(currentFullPath)
     }
   }
 
-  // 当历史记录长度大于1时可以返回
+  // 是否可以返回
   canGoBack.value = navigationHistory.value.length > 1
 })
 
@@ -358,12 +323,12 @@ const backHandler = async () => {
     // 移除当前路径
     navigationHistory.value.pop()
 
-    // 设置返回导航标志
+    // 设置返回标志
     isBackNavigation = true
 
-    // 返回到上一个路径
-    const prevPath = navigationHistory.value[navigationHistory.value.length - 1]
-    await router.push(prevPath)
+    // 跳转到上一个完整路径(包含参数)
+    const prevFullPath = navigationHistory.value[navigationHistory.value.length - 1]
+    await router.push(prevFullPath)
   }
 }