fix: 路由切换后滚动到顶部

详情页→列表→另一个详情页时滚动位置保留在前一页的偏移,
用 router.afterEach 统一处理,每次导航回到顶部。
This commit is contained in:
崮生(子虚) 2026-07-31 00:06:05 +08:00
parent 3e3e8899d5
commit 98c3c5a6e2

View File

@ -34,6 +34,18 @@ export const createApp = ViteSSG(
({ router }) => {
/** 显式使用 history 模式,避免 hash 模式下 SSG 路由不生效 */
router.options.history = createWebHistory(import.meta.env.BASE_URL);
/**
*
*
* Vue Router SPA
*
*/
router.afterEach((_to, _from) => {
if (typeof window === "undefined") return;
/** nextTick 确保 DOM 已更新到新页面后再滚动 */
window.scrollTo({ top: 0 });
});
},
);