如何停止在angular5中合并

how to stop pooling in angular5

service.ts:

   // Get all AgentLog log using pooling
     getAgentLogStream(): Promise<string> {
       const url = `${this.testCaseUrl}/logfile`;
       return  Observable
       .interval(5000).flatMap((i)=>
        this.http.get(url)
       .toPromise()
       .then(response => response.text())
       .catch(this.handleError));
}

component.ts:

getAgentLogStream1() {
    return this.AgentService.getAgentLogStream()
    .subscribe( agentLogStream => this.agentLogStream = agentLogStream
            , error => console.log(error));
}

我正在使用上面的服务代码来进行休息调用和相应的组件代码。

下面是同一组件中控制合并数据的代码:

 //To auto refresh the logs 
onChange( event ) {
    console.log( 'Event', event )
    if ( event.target.checked ) {
        this.getAgentLogStream1();
    } 
}

我能够获取定期刷新的数据,但即使在导航到其他 page.need 后也无法停止它 关于如果我导航到其他页面或切换时如何停止的帮助 UI。

在组件内部的某个变量中存储订阅:

this.streamSubscription = this.getAgentLogStream1();

那么如果你想取消这个订阅电话:

streamSubscription.unsubscribe();

在你的组件内部它看起来像这样:

export class SomeComponent  {
  //...
  streamSubscription;

  ngOnInit() {
    this.streamSubscription = this.getAgentLogStream1();
  }

  getAgentLogStream1(): Promise<string> {
   return this.AgentService.getAgentLogStream()
     .subscribe(
       agentLogStream => this.agentLogStream = agentLogStream,
       error => console.log(error)
     );
  }

  functionToCallIfYouWantToCancelSubscription() {
    this.streamSubscription.unsubscribe();
  }
}