6

I'm working on a project in which I have a lot of routes and each time I click on a link to go to a specific route the page does not scroll to top automatically which is annoying and not good. Does anyone know how to fix this problem, please?

SaboSuke
  • 634
  • 6
  • 21

1 Answers1

15

Add the following configuration to the routing module in the extra options:

const routes: Routes = [ ... ];

@NgModule({
  imports: [RouterModule.forRoot(routes, {
    scrollPositionRestoration: 'enabled'
  })],
  exports: [RouterModule]
})
export class AppRoutingModule { }

For your interests, the option scrollPositionRestoration can be top (go to top on every navigation change) or enabled (like top, but when going backwards it restores the last position).

More info: https://angular.io/api/router/ExtraOptions

Iván Pérez
  • 2,278
  • 1
  • 24
  • 49