Commit fef3644067b7ccac96ec9ae122e3f1c8b8fc58ef

Authored by vben
1 parent f4621cc6

fix: page switching did not return to the top

src/router/guard/index.ts
1 -import { isNavigationFailure, Router } from 'vue-router'; 1 +import { isNavigationFailure, RouteLocationNormalized, Router } from 'vue-router';
2 2
3 import { Modal, notification } from 'ant-design-vue'; 3 import { Modal, notification } from 'ant-design-vue';
4 4
@@ -19,6 +19,12 @@ import { REDIRECT_NAME } from '/@/router/constant'; @@ -19,6 +19,12 @@ import { REDIRECT_NAME } from '/@/router/constant';
19 const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting(); 19 const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting();
20 const globSetting = useGlobSetting(); 20 const globSetting = useGlobSetting();
21 21
  22 +const body = document.body;
  23 +
  24 +const isHash = (href: string) => {
  25 + return /^#/.test(href);
  26 +};
  27 +
22 export function createGuard(router: Router) { 28 export function createGuard(router: Router) {
23 let axiosCanceler: Nullable<AxiosCanceler>; 29 let axiosCanceler: Nullable<AxiosCanceler>;
24 if (removeAllHttpPending) { 30 if (removeAllHttpPending) {
@@ -45,8 +51,13 @@ export function createGuard(router: Router) { @@ -45,8 +51,13 @@ export function createGuard(router: Router) {
45 }); 51 });
46 52
47 router.afterEach((to, from, failure) => { 53 router.afterEach((to, from, failure) => {
  54 + // scroll top
  55 + isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0);
  56 +
48 loadedPageMap.set(to.path, true); 57 loadedPageMap.set(to.path, true);
  58 +
49 const { t } = useI18n(); 59 const { t } = useI18n();
  60 +
50 // change html title 61 // change html title
51 to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title); 62 to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title);
52 63
src/router/index.ts
@@ -8,9 +8,11 @@ import { createGuard } from &#39;./guard/&#39;; @@ -8,9 +8,11 @@ import { createGuard } from &#39;./guard/&#39;;
8 import { basicRoutes } from './routes/'; 8 import { basicRoutes } from './routes/';
9 import { scrollBehavior } from './scrollBehaviour'; 9 import { scrollBehavior } from './scrollBehaviour';
10 10
  11 +export const hashRouter = createWebHashHistory();
  12 +
11 // app router 13 // app router
12 const router = createRouter({ 14 const router = createRouter({
13 - history: createWebHashHistory(), 15 + history: hashRouter,
14 routes: basicRoutes as RouteRecordRaw[], 16 routes: basicRoutes as RouteRecordRaw[],
15 strict: true, 17 strict: true,
16 scrollBehavior: scrollBehavior, 18 scrollBehavior: scrollBehavior,
src/router/scrollBehaviour.ts
  1 +// see https://github.com/vuejs/vue-router-next/blob/master/playground/scrollWaiter.ts
  2 +import type { RouteLocationNormalized } from 'vue-router';
  3 +// class ScrollQueue {
  4 +// private resolve: (() => void) | null = null;
  5 +// private promise: Promise<any> | null = null;
  6 +
  7 +// add() {
  8 +// this.promise = new Promise((resolve) => {
  9 +// this.resolve = resolve as () => void;
  10 +// });
  11 +// }
  12 +
  13 +// flush() {
  14 +// this.resolve && this.resolve();
  15 +// this.resolve = null;
  16 +// this.promise = null;
  17 +// }
  18 +
  19 +// async wait() {
  20 +// await this.promise;
  21 +// }
  22 +// }
  23 +// const scrollWaiter = new ScrollQueue();
  24 +
1 /** 25 /**
2 * Handles the scroll behavior on route navigation 26 * Handles the scroll behavior on route navigation
3 * 27 *
@@ -8,10 +32,9 @@ @@ -8,10 +32,9 @@
8 */ 32 */
9 // @ts-ignore 33 // @ts-ignore
10 export async function scrollBehavior(to, from, savedPosition) { 34 export async function scrollBehavior(to, from, savedPosition) {
11 - await scrollWaiter.wait(); 35 + // await scrollWaiter.wait();
12 // Use predefined scroll behavior if defined, defaults to no scroll behavior 36 // Use predefined scroll behavior if defined, defaults to no scroll behavior
13 - const behavior = document.documentElement.style.scrollBehavior || 'auto';  
14 - 37 + const behavior = 'smooth';
15 // Returning the `savedPosition` (if available) will result in a native-like 38 // Returning the `savedPosition` (if available) will result in a native-like
16 // behavior when navigating with back/forward buttons 39 // behavior when navigating with back/forward buttons
17 if (savedPosition) { 40 if (savedPosition) {
@@ -24,7 +47,7 @@ export async function scrollBehavior(to, from, savedPosition) { @@ -24,7 +47,7 @@ export async function scrollBehavior(to, from, savedPosition) {
24 } 47 }
25 48
26 // Check if any matched route config has meta that discourages scrolling to top 49 // Check if any matched route config has meta that discourages scrolling to top
27 - if (to.matched.some((m: any) => m.meta.scrollToTop === false)) { 50 + if (to.matched.some((m: RouteLocationNormalized) => m.meta.scrollToTop === false)) {
28 // Leave scroll as it is 51 // Leave scroll as it is
29 return false; 52 return false;
30 } 53 }
@@ -32,27 +55,3 @@ export async function scrollBehavior(to, from, savedPosition) { @@ -32,27 +55,3 @@ export async function scrollBehavior(to, from, savedPosition) {
32 // Always scroll to top 55 // Always scroll to top
33 return { left: 0, top: 0, behavior }; 56 return { left: 0, top: 0, behavior };
34 } 57 }
35 -  
36 -// see https://github.com/vuejs/vue-router-next/blob/master/playground/scrollWaiter.ts  
37 -class ScrollQueue {  
38 - private resolve: (() => void) | null = null;  
39 - private promise: Promise<any> | null = null;  
40 -  
41 - add() {  
42 - this.promise = new Promise((resolve) => {  
43 - this.resolve = resolve as () => void;  
44 - });  
45 - }  
46 -  
47 - flush() {  
48 - this.resolve && this.resolve();  
49 - this.resolve = null;  
50 - this.promise = null;  
51 - }  
52 -  
53 - async wait() {  
54 - await this.promise;  
55 - }  
56 -}  
57 -  
58 -export const scrollWaiter = new ScrollQueue();