最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 9 scroll to top on route every change - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Jun 11, 2020 at 12:26 SaboSukeSaboSuke 7921 gold badge8 silver badges27 bronze badges 5
  • Does this answer your question? Angular 2 Scroll to top on Route Change – Philipp Meissner Commented Jun 11, 2020 at 12:29
  • No sorry I already tried that. – SaboSuke Commented Jun 11, 2020 at 12:32
  • I think I found a great answer to this question it's right here – SaboSuke Commented Jun 11, 2020 at 12:38
  • Quoting that response, "Since Angular6.1, we can also use { scrollPositionRestoration: 'enabled' } on eagerly loaded modules and it will be applied to all routes" – Iván Pérez Commented Jun 11, 2020 at 12:43
  • Yes that will work too. – SaboSuke Commented Jun 11, 2020 at 13:00
Add a comment  | 

1 Answer 1

Reset to default 18

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

发布评论

评论列表(0)

  1. 暂无评论