Browse Source

当页面跳转的时候让新页面滚动到页面顶部

gcz 1 week ago
parent
commit
63935cab39
1 changed files with 8 additions and 0 deletions
  1. 8 0
      src/router/index.js

+ 8 - 0
src/router/index.js

@@ -37,6 +37,14 @@ const routes = [
 const router = createRouter({
 const router = createRouter({
   history: createWebHistory(),
   history: createWebHistory(),
   routes,
   routes,
+  scrollBehavior(to, from, savedPosition) {
+    // 如果有保存的位置(比如浏览器前进后退),则返回到保存的位置
+    if (savedPosition) {
+      return savedPosition
+    }
+    // 否则滚动到页面顶部
+    return { top: 0 }
+  }
 })
 })
 
 
 export default router
 export default router