Autoscroll in Angular 2

后端 未结 10 1185
清歌不尽
清歌不尽 2020-12-01 12:21

I\'m experiencing an issue with Angular 2 where changing from one route to another does not automatically scroll to the top of the new view. I realize that Angular 1 allowed

10条回答
  •  温柔的废话
    2020-12-01 13:09

    I have used if(this.router.navigated) in the ngOnInit of each page to determine whether or not to use window.scrollTo(0, 0). This will cover most cases of routing to the page, while leaving the scroll position where it should be if you click the browser Back button.

    if(this.router.navigated) {
      window.scrollTo(0, 0);
    }
    

提交回复
热议问题