From d0cc9005b0ace640200384c64d352b4f04d850d7 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Mon, 24 Apr 2023 18:00:55 +0800 Subject: [PATCH] perf: improve router scrollBehavior configuration (#3834) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area console /milestone 2.5.x #### What this PR does / why we need it: 修改 Vue Router 的 scrollBehavior 配置,添加是否是不同路由的跳转,如果是才会在跳转之后滚动到页面顶部。可以解决在 https://github.com/halo-dev/halo/issues/3833 中提到的打开附件详情之后,列表滚动到顶部的问题。这是因为在打开附件详情的时候会修改路由的参数。 #### Which issue(s) this PR fixes: Fixes #3833 #### Special notes for your reviewer: 测试方式: 1. 上传若干附件,滚动到列表底部。 2. 然后打开某个附件详情,观察列表是否自动滚动到了顶部,如果没有,则修改有效。 #### Does this PR introduce a user-facing change? ```release-note None ``` --- console/src/router/index.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/console/src/router/index.ts b/console/src/router/index.ts index ce86dac46..db3f74895 100644 --- a/console/src/router/index.ts +++ b/console/src/router/index.ts @@ -1,4 +1,9 @@ -import { createRouter, createWebHashHistory } from "vue-router"; +import { + createRouter, + createWebHashHistory, + type RouteLocationNormalized, + type RouteLocationNormalizedLoaded, +} from "vue-router"; import routesConfig from "@/router/routes.config"; import { setupPermissionGuard } from "./guards/permission"; import { setupCheckStatesGuard } from "./guards/check-states"; @@ -7,7 +12,14 @@ import { setupAuthCheckGuard } from "./guards/auth-check"; const router = createRouter({ history: createWebHashHistory(import.meta.env.BASE_URL), routes: routesConfig, - scrollBehavior: () => ({ left: 0, top: 0 }), + scrollBehavior: ( + to: RouteLocationNormalized, + from: RouteLocationNormalizedLoaded + ) => { + if (to.name !== from.name) { + return { left: 0, top: 0 }; + } + }, }); setupAuthCheckGuard(router);