优化菜单切换导致浏览器刷新问题

This commit is contained in:
guanj
2026-07-09 15:40:56 +08:00
parent 8bca4191f2
commit ee9cd057b8
6 changed files with 233 additions and 193 deletions

View File

@@ -94,9 +94,11 @@ const init = async () => {
return return
} }
} }
// 跳转到第一个菜单 // 仅在 loading 页时跳转到第一个菜单,避免覆盖用户当前页面
let firstRoute = getFirstRoute(navTabs.state.tabsViewRoutes) if (route.name === 'adminMainLoading') {
if (firstRoute) routePush(firstRoute.path) const firstRoute = getFirstRoute(navTabs.state.tabsViewRoutes)
if (firstRoute) routePush(firstRoute.path)
}
}) })
} }

View File

@@ -1,9 +1,9 @@
<template> <template>
<el-main class="layout-main" :style="mainHeight()"> <el-main class="layout-main" :style="mainHeight()">
<router-view v-slot="{ Component }"> <router-view v-slot="{ Component }">
<transition :name="config.layout.mainAnimation" mode="out-in"> <transition :name="config.layout.mainAnimation">
<keep-alive :include="state.keepAliveComponentNameList"> <keep-alive :include="state.keepAliveComponentNameList">
<component :is="Component" :key="state.componentKey" /> <component :is="Component" :key="state.componentKey || undefined" />
</keep-alive> </keep-alive>
</transition> </transition>
</router-view> </router-view>
@@ -32,7 +32,7 @@ const state: {
componentKey: string componentKey: string
keepAliveComponentNameList: string[] keepAliveComponentNameList: string[]
} = reactive({ } = reactive({
componentKey: route.path, componentKey: '',
keepAliveComponentNameList: [] keepAliveComponentNameList: []
}) })
@@ -79,7 +79,6 @@ onMounted(() => {
watch( watch(
() => route.path, () => route.path,
() => { () => {
state.componentKey = route.path
if (typeof navTabs.state.activeRoute?.meta.keepalive == 'string') { if (typeof navTabs.state.activeRoute?.meta.keepalive == 'string') {
addKeepAliveComponentName(navTabs.state.activeRoute?.meta.keepalive) addKeepAliveComponentName(navTabs.state.activeRoute?.meta.keepalive)
} }

View File

@@ -5,7 +5,7 @@
element-loading-background="var(--ba-bg-color-overlay)" element-loading-background="var(--ba-bg-color-overlay)"
element-loading-text="加载中" element-loading-text="加载中"
class="default-main ba-main-loading" class="default-main ba-main-loading"
:style="{ height:'calc(100vh - 170px)'}" :style="{ height: 'calc(100vh - 170px)' }"
></div> ></div>
<div v-if="state.showReload" class="loading-footer mt10"> <div v-if="state.showReload" class="loading-footer mt10">
<el-button @click="refresh" type="warning">重新加载</el-button> <el-button @click="refresh" type="warning">重新加载</el-button>
@@ -14,26 +14,65 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { onUnmounted, reactive } from 'vue' import { onMounted, onUnmounted, reactive, watch } from 'vue'
import { useRoute } from 'vue-router'
import router from '@/router/index' import router from '@/router/index'
import { useNavTabs } from '@/stores/navTabs' import { useNavTabs } from '@/stores/navTabs'
import { isAdminApp } from '@/utils/common' import { isAdminApp } from '@/utils/common'
import { getFirstRoute, routePush } from '@/utils/router' import { getFirstRoute, routePush } from '@/utils/router'
import { isEmpty } from 'lodash-es'
import { adminBaseRoutePath } from '@/router/static'
let timer: number let timer: number
const route = useRoute()
const navTabs = useNavTabs() const navTabs = useNavTabs()
const state = reactive({ const state = reactive({
maximumWait: 1000 * 6, maximumWait: 1000 * 6,
showReload: false, showReload: false
}) })
const refresh = () => { const tryRedirect = () => {
router.go(0) if (route.params.to) {
try {
const lastRoute = JSON.parse(route.params.to as string)
if (lastRoute.path && lastRoute.path !== adminBaseRoutePath) {
const query = !isEmpty(lastRoute.query) ? lastRoute.query : {}
routePush({ path: lastRoute.path, query })
return true
}
} catch (error) {
console.error(error)
}
}
if (isAdminApp() && navTabs.state.tabsViewRoutes.length > 0) {
const firstRoute = getFirstRoute(navTabs.state.tabsViewRoutes)
if (firstRoute) {
routePush(firstRoute.path)
return true
}
}
return false
} }
// if (isAdminApp() && navTabs.state.tabsViewRoutes.length > 0) {
// let firstRoute = getFirstRoute(navTabs.state.tabsViewRoutes) const refresh = () => {
// if (firstRoute) routePush(firstRoute.path) if (!tryRedirect()) {
// } router.replace(route.fullPath)
}
}
onMounted(() => {
if (tryRedirect()) return
const stop = watch(
() => navTabs.state.tabsViewRoutes.length,
len => {
if (len > 0 && tryRedirect()) {
stop()
}
}
)
})
timer = window.setTimeout(() => { timer = window.setTimeout(() => {
state.showReload = true state.showReload = true

View File

@@ -346,7 +346,7 @@ const staticRoutes: Array<RouteRecordRaw> = [
{ {
// 后台找不到页面了-可能是路由未加载上 // 后台找不到页面了-可能是路由未加载上
path: adminBaseRoutePath + ':path(.*)*', path: adminBaseRoutePath + '/:path(.*)*',
redirect: to => { redirect: to => {
return { return {
name: 'adminMainLoading', name: 'adminMainLoading',

View File

@@ -179,7 +179,6 @@ function createAxios<Data = any, T = ApiPromise<Data>>(
return Axios(response.config) return Axios(response.config)
}) })
.catch(err => { .catch(err => {
window.location.reload()
adminInfo.removeToken() adminInfo.removeToken()
router.push({ name: 'login' }) router.push({ name: 'login' })
return Promise.reject(err) return Promise.reject(err)

View File

@@ -70,6 +70,7 @@ export const onClickMenu = (menu: RouteRecordRaw) => {
switch (menu.meta?.menu_type) { switch (menu.meta?.menu_type) {
case 'iframe': case 'iframe':
case 'tab': case 'tab':
if (router.currentRoute.value.path === menu.path) return
routePush({ path: menu.path }) routePush({ path: menu.path })
break break
case 'link': case 'link':