mirror of
https://github.com/2234839/web-font.git
synced 2026-09-16 14:05:42 +08:00
fix: 路由切换后滚动到顶部
详情页→列表→另一个详情页时滚动位置保留在前一页的偏移, 用 router.afterEach 统一处理,每次导航回到顶部。
This commit is contained in:
parent
3e3e8899d5
commit
98c3c5a6e2
12
src/main.ts
12
src/main.ts
@ -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 });
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user