diff --git a/src/App.vue b/src/App.vue index f5326d59..e834d2d6 100644 --- a/src/App.vue +++ b/src/App.vue @@ -7,9 +7,6 @@ import store from './store' const { global: globalTheme } = useTheme() globalTheme.name.value = localStorage.getItem('theme') || 'light' -// 路由 -const route = useRoute() - // 提示框 const $toast = useToast() diff --git a/src/main.ts b/src/main.ts index 03358e6d..38743456 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,9 +30,6 @@ app.use(vuetify) }) .use(VuetifyUseDialog).mount('#app') -// 记录和恢复滚动位置 -const scrollPositions: { [key: string]: number } = {} // 用于存储每个路由的滚动位置 - // 路由导航守卫 router.beforeEach((to, from, next) => { const isAuthenticated = store.state.auth.token !== null @@ -41,24 +38,11 @@ router.beforeEach((to, from, next) => { next('/login') } else { - // 只有 meta 中 keepAlive 为 true 的情况下才记录滚动位置 - if (from.meta.keepAlive) - scrollPositions[from.fullPath] = window.scrollY - startNProgress() next() } }) router.afterEach((to) => { - // 只有 meta 中 keepAlive 为 true 的情况下才恢复滚动位置 - if (to.meta.keepAlive) { - const savedPosition = scrollPositions[to.fullPath] - if (savedPosition !== undefined) { - setTimeout(() => { - window.scrollTo(0, savedPosition) - }, 0) - } - } doneNProgress() }) diff --git a/src/pages/login.vue b/src/pages/login.vue index 0421e999..5777649f 100644 --- a/src/pages/login.vue +++ b/src/pages/login.vue @@ -130,7 +130,7 @@ onMounted(() => { diff --git a/src/router/index.ts b/src/router/index.ts index 6882cfe4..2a1402e7 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -2,8 +2,11 @@ import { createRouter, createWebHistory } from 'vue-router' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), - scrollBehavior() { - // 始终滚动到顶部 + scrollBehavior(to, from, savedPosition) { + // 如果页面有缓存那么恢复其位置, 否则始终滚动到顶部 + if (to.meta.keepAlive && savedPosition) + return savedPosition + // console.log('top: 0') return { top: 0 } }, routes: [