title: Destroy Angular 5 components when move to another menu
body: <p>I have a report component which have Behavior Subject subscription and it pull data from web api after .next call.
  When I am move to another page after report page, report component still alive and keeps sending calls to webApi.</p>
  
  <p>How to destroy components after navigate to another or unsubscribe Behavior Subjects.
  More details.
  I have few report components, it renders different type of reports. One of the component is below  </p>
  
  <pre><code>@destroyAllSubscribers()
      export class StandardReportComponent implements OnInit {
  
      public subscriptions: Subscription[] = [];
      reportData = [];
      showReport=false;
      constructor(private reportService: ReportService)
      {
  
          this.subscriptions.push(this.reportService.chartType.subscribe(chartType =&gt; {
  
          if (this.currentChart != ReportChartType.None){
  
           this.showReport=false;  //Used in *ngIf to show report HTML template
              this.reportService.getReportData().subscribe(result=&gt;{
          this.reportData=result; 
          this.showReport=true; //Used in *ngIf to show report HTML template
  
  
      }
  
      }
  
      }
      }
  </code></pre>
  
  <p>I have destroy subscriber decorator which gets executed when component destroys,  </p>
  
  <p>Code:</p>
  
  <pre><code>export function destroyAllSubscribers() {
      return (targetComponent: any) =&gt; {
        targetComponent.prototype.ngOnDestroy = ngOnDestroyDecorator();
  
        function ngOnDestroyDecorator() {
          return function () {
            if (this.subscriptions != undefined)
            {
              this.subscriptions.forEach(subscription =&gt; {
                if (subscription instanceof Subscriber) {
                  subscription.unsubscribe();
                };
              });
            }
          };
        }
      };
  }
  </code></pre>
  
  <p>It should unsubscribe; however all subscription gets executed after navigate to other page as well.</p>