Angular 5 Scroll to top on every Route click

后端 未结 18 1922
遇见更好的自我
遇见更好的自我 2020-11-29 17:49

I am using angular 5. I have a dashboard where I have few sections with small content and few sections with so large content that I am facing a problem when changing router

18条回答
  •  天涯浪人
    2020-11-29 18:28

    Here is a solution that only scrolls to top of Component if first time visiting for EACH component (in case you need to do something different per component):

    In each Component:

    export class MyComponent implements OnInit {
    
    firstLoad: boolean = true;
    
    ...
    
    ngOnInit() {
    
      if(this.firstLoad) {
        window.scroll(0,0);
        this.firstLoad = false;
      }
      ...
    }
    

提交回复
热议问题