mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): scroll to top on dynamic routes with different params (#8327)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
parent
418b5671ff
commit
0106e09e32
@ -1,5 +1,6 @@
|
||||
import type { RouterConfig } from '@nuxt/schema'
|
||||
import type { RouterScrollBehavior } from 'vue-router'
|
||||
import type { RouterScrollBehavior, RouteLocationNormalized } from 'vue-router'
|
||||
import { isEqual } from 'ohash'
|
||||
import { nextTick } from 'vue'
|
||||
import { useNuxtApp } from '#app'
|
||||
|
||||
@ -16,11 +17,7 @@ export default <RouterConfig> {
|
||||
let position: ScrollPosition = savedPosition || undefined
|
||||
|
||||
// Scroll to top if route is changed by default
|
||||
if (
|
||||
!position &&
|
||||
(from && to && from.matched[0] !== to.matched[0]) &&
|
||||
to.meta.scrollToTop !== false
|
||||
) {
|
||||
if (!position && from && to && to.meta.scrollToTop !== false && _isDifferentRoute(from, to)) {
|
||||
position = { left: 0, top: 0 }
|
||||
}
|
||||
|
||||
@ -56,3 +53,14 @@ function _getHashElementScrollMarginTop (selector: string): number {
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
function _isDifferentRoute (a: RouteLocationNormalized, b: RouteLocationNormalized): boolean {
|
||||
const samePageComponent = a.matched[0] === b.matched[0]
|
||||
if (!samePageComponent) {
|
||||
return true
|
||||
}
|
||||
if (samePageComponent && !isEqual(a.params, b.params)) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user