Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何停止angular5中的池_Typescript_Observable_Angular2 Forms_Angular Promise_Angular5 - Fatal编程技术网

Typescript 如何停止angular5中的池

Typescript 如何停止angular5中的池,typescript,observable,angular2-forms,angular-promise,angular5,Typescript,Observable,Angular2 Forms,Angular Promise,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)

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));
}
我正在使用上面的代码进行rest调用服务和相应的组件代码

以下是同一组件中控制池数据的代码:

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

我能够获得定期刷新的数据,但即使在导航到其他页面后也无法停止数据。如果我导航到其他页面或通过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();
  }
}
导出类组件{
//...
流动认购;
恩戈尼尼特(){
this.streamSubscription=this.getAgentLogStream1();
}
getAgentLogStream1():承诺{
返回此.AgentService.getAgentLogStream()
.订阅(
agentLogStream=>this.agentLogStream=agentLogStream,
error=>console.log(错误)
);
}
函数callifyouwanttocancelsubscription(){
this.streamSubscription.unsubscribe();
}
}

那么,我是否需要从getAgentLogStream1()中完全删除订阅部分,能否请您提供更多详细信息(我重命名了函数并保存以避免混淆),能否请您提供更多的代码说明..我编辑了我的答案,如果有不清楚的地方,请告诉我
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();
  }
}