Autoscroll in Angular 2

后端 未结 10 1189
清歌不尽
清歌不尽 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:06

    I had the same issue. Based on Gunter's answer I found that Angular's 2 RC.1 new router doesn't expose an Observable directly. Instead it has a changes property for that purpose. The workaround for RC.1 is:

    this._router.changes.subscribe(() => {
        window.scrollTo(0, 0);
    }); 
    

提交回复
热议问题