scrollGuard.ts 387 B

123456789101112131415
  1. import type { RouteLocationNormalized, Router } from 'vue-router';
  2. const isHash = (href: string) => {
  3. return /^#/.test(href);
  4. };
  5. export function createScrollGuard(router: Router) {
  6. const body = document.body;
  7. router.afterEach(async (to) => {
  8. // scroll top
  9. isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0);
  10. return true;
  11. });
  12. }