From e72aea100e1760e4acd7bbb5038272877c04e7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=88=90?= Date: Thu, 9 Oct 2025 18:34:49 +0800 Subject: [PATCH] 1 --- demo/src/main.js | 23 ++++++++++++++--------- src/store/user.js | 17 +++++++++++++++++ src/views/login/login.vue | 10 ++++++++-- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/demo/src/main.js b/demo/src/main.js index 8624efa..a91c1e9 100644 --- a/demo/src/main.js +++ b/demo/src/main.js @@ -44,19 +44,14 @@ const app = new Vue({ console.log('框架版本:', AdminFramework.version) console.log('配置信息:', this.$config) - // 设置系统标题 - await this.$store.dispatch('app/getSysTitle', { - defaultTitle: this.$config.title, - defaultLogo: '' - }) - - // 刷新时恢复菜单 + // 刷新时恢复菜单和标题 const token = this.$store.state.user.token const authorityMenus = localStorage.getItem('authorityMenus') if (token && authorityMenus) { - console.log('检测到登录状态,恢复菜单...') + console.log('检测到登录状态,恢复菜单和标题...') try { + // 先恢复菜单 await this.$store.dispatch('user/setAuthorityMenus', { Main: AdminFramework.Main, ParentView: AdminFramework.ParentView, @@ -64,9 +59,19 @@ const app = new Vue({ authorityMenus: authorityMenus }) console.log('菜单恢复成功') + + // 再获取系统标题(已登录,会从接口获取) + await this.$store.dispatch('app/getSysTitle', { + defaultTitle: this.$config.title, + defaultLogo: '' + }) } catch (error) { - console.error('恢复菜单失败:', error) + console.error('恢复失败:', error) } + } else { + // 未登录,使用默认标题 + console.log('未登录,使用默认标题') + document.title = this.$config.title } } }) diff --git a/src/store/user.js b/src/store/user.js index b332631..cec5c6d 100644 --- a/src/store/user.js +++ b/src/store/user.js @@ -100,6 +100,23 @@ export default { if (mainMenu && mainMenu.children) { commit('setMenuList', mainMenu.children) + + // 动态添加路由(重要!解决登录后点击菜单空白的问题) + if (window.rootVue && window.rootVue.$router) { + // 先移除旧的主路由(如果存在) + const router = window.rootVue.$router + const routes = router.options.routes + + // 查找并移除旧的主路由 + const mainRouteIndex = routes.findIndex(r => r.path === '/') + if (mainRouteIndex !== -1) { + routes.splice(mainRouteIndex, 1) + } + + // 添加新的主路由 + router.addRoute(mainMenu) + console.log('动态路由已添加') + } } }, async handleLogin({ state, commit, dispatch }, { userFrom, Main, ParentView, Page404 }) { diff --git a/src/views/login/login.vue b/src/views/login/login.vue index d8fce66..b90a537 100644 --- a/src/views/login/login.vue +++ b/src/views/login/login.vue @@ -53,9 +53,15 @@ export default { Page404 }) - // 跳转到首页(使用 location.href 触发完整页面加载) + this.$Message.success('登录成功!') + + // 跳转到首页 + // 使用 replace 避免返回时回到登录页 setTimeout(() => { - window.location.href = window.location.origin + window.location.pathname + '#/' + this.$router.replace({ path: '/home' }).catch(() => { + // 如果路由跳转失败,刷新页面 + window.location.href = window.location.origin + window.location.pathname + '#/' + }) }, 500) } catch (error) { console.error('登录失败:', error)