End Observable interval when route changes in Angular

前端 未结 1 668
无人共我
无人共我 2021-01-14 20:56

I start a interval in an Angular component, but it keeps making requests even after I change the route. How can I stop the interval?

//returns an observable
         


        
相关标签:
1条回答
  • 2021-01-14 21:26

    You should save subscription to observable:

    this.subscription = getAllPolls().subscribe(...);
    

    And implement OnDestroy interface:

      ngOnDestroy() {
         this.subscription.unsubscribe();
      }
    
    0 讨论(0)
提交回复
热议问题