Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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
Angular 让RxJS Observable立即返回初始数据,然后每隔X秒重复一次?_Angular_Rxjs_Observable - Fatal编程技术网

Angular 让RxJS Observable立即返回初始数据,然后每隔X秒重复一次?

Angular 让RxJS Observable立即返回初始数据,然后每隔X秒重复一次?,angular,rxjs,observable,Angular,Rxjs,Observable,下面是一个代码示例,它每30秒给我一次结果,但是,当组件订阅服务时,它还必须等待30秒才能获得初始数据集: getTasks(query: string): Observable<any> { return this._http.get(this._ripcord + '/tasks' + query, this.getHttpOptions()) .map((response: Response) => <any>response.json())

下面是一个代码示例,它每30秒给我一次结果,但是,当组件订阅服务时,它还必须等待30秒才能获得初始数据集:

getTasks(query: string): Observable<any> {
  return this._http.get(this._ripcord + '/tasks' + query, this.getHttpOptions())
    .map((response: Response) => <any>response.json())
    .delay(30000)
    .repeat()
    .do(data => console.log(data))
    .catch(this.handleError);
}
gettask(query:string):可以观察到,但这个解决方案对我不起作用

有人知道我可以组合使用什么运算符在订阅时立即检索初始数据吗?

interval.startWith(如您所指的解决方案中所引用的)确实是答案

我怀疑你真正想要的是:

Observable
  .interval(30000)
  .startWith(0)
  .map(event =>
    this._http.get(this._ripcord + '/tasks' + query, this.getHttpOptions())
  )
  .do(data => console.log(data))
  .catch(this.handleError);
你可以用。使用定时器,您可以指定何时开始发射。以下示例立即发出第一个值,然后需要5秒才能发出后续值

var sourceTimer = Rx.Observable
        .timer(0, 5000)
        .timeInterval()
        .take(10);

var subscriptionTimer = sourceTimer.subscribe(
        function (x) {
            console.log('Next: ' + JSON.stringify(x));
        },
        function (err) {
            console.log('Error: ' + err);
        },
        function () {
            console.log('Completed');
        });
输出:

Next: {"value":0,"interval":4}
Next: {"value":1,"interval":5004}
Next: {"value":2,"interval":5000}
Next: {"value":0,"interval":1}
Next: {"value":0,"interval":5003}
Next: {"value":1,"interval":5000}

Next: {"value":7,"interval":5000}
Next: {"value":8,"interval":5001}
&也可以工作,立即将作为参数提供给startWith的值0发送到startWith,然后在提供的时间间隔内发送后续值

  var source = Rx.Observable
    .interval( 5000 /* ms */)
    .startWith(0)
    .timeInterval()
    .take(10);

 var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + JSON.stringify(x));
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });
输出:

Next: {"value":0,"interval":4}
Next: {"value":1,"interval":5004}
Next: {"value":2,"interval":5000}
Next: {"value":0,"interval":1}
Next: {"value":0,"interval":5003}
Next: {"value":1,"interval":5000}

Next: {"value":7,"interval":5000}
Next: {"value":8,"interval":5001}

如果您需要在http请求返回错误时保持可观察序列运行,那么在发出请求时捕获错误并返回空的可观察序列

  getTasks(query: string): Observable<any> {
    return Observable
      .interval(30000)
      .startWith(0)
      .switchMap(event => {
        this._http
          .get(this._ripcord + '/tasks' + query, this.getHttpOptions())
          .catch(error => Observable.empty());
      })
      .map((response: Response) => <any>response.json())
      .do(data => console.log(data))
      .catch(this.handleError);
  }
gettask(查询:字符串):可观察{
可观测回波
.间隔(30000)
.startWith(0)
.switchMap(事件=>{
这个
.get(this._ripcord+'/tasks'+query,this.getHttpOptions())
.catch(error=>Observable.empty());
})
.map((response:response)=>response.json())
.do(数据=>console.log(数据))
.接住(这个.把手错误);
}

否则,序列将在第一个错误时停止。

感谢大家的回答,因为他们帮助我创建了自己的解决方案

getTasks(query: string): Observable<any> {
  return Observable
    .timer(0, 30000)
    .switchMap(() =>
      this._http.get(this._ripcord + '/tasks' + query, this.getHttpOptions())
        .map((response: Response) => <any>response.json())
        .do(data => console.log(data))
        .catch(this.handleError)
    );
}
gettask(查询:字符串):可观察{
可观测回波
.计时器(0,30000)
.switchMap(()=>
this.\u http.get(this.\u ripcord+'/tasks'+query,this.getHttpOptions())
.map((response:response)=>response.json())
.do(数据=>console.log(数据))
.接住(这个.把手错误)
);
}

gettask(查询:字符串):可观察{
可观测回波
.间隔(30000)
.startWith(0)
.switchMap(()=>
this.\u http.get(this.\u ripcord+'/tasks'+query,this.getHttpOptions())
.map((response:response)=>response.json())
.do(数据=>console.log(数据))
.接住(这个.把手错误)
);
}

gettask(查询:字符串):可观察{
可观测回波
.计时器(0,30000)
.switchMap(()=>
this.\u http.get(this.\u ripcord+'/tasks'+query,this.getHttpOptions())
.map((response:response)=>response.json())
)
.do(数据=>console.log(数据))
.接住(这个.把手错误);
}

我意识到我需要利用switchMap,因为map在对gettask端点的Http服务GET响应的订阅中返回了一个可观察的值。您可以选择将DO和CATCH操作符放在switchMap上,而不是map操作符上。

这个问题的解决方案应该在这里起作用。怎么了?订阅返回的是整数而不是对象!您必须使用
flatMap
或类似的工具(将您的原始请求放入映射中),正如一些答案所示。计时器已被弃用,取而代之的是intervalI。我注意到您还使用了timeInterval操作符。同时拥有timer(interval)和timeInterval的目的是什么?我也不知道timer在哪里被弃用。我不清楚这个选项,它会使我的应用程序崩溃,并导致TypeError:无法读取的属性“data”undefined@AndrewLobban您必须检查是否发出null并处理它。重要的一点是,只有在可观测链的末端有一个捕获,这意味着如果_http.get失败,可观测将停止。通过处理错误,然后返回一个可观察值,它将继续每30秒发出一次请求,谢谢!然而,我意识到地图并没有返回我的数据对象,而是另一个可观察对象,所以我需要将可观察对象与switchMap合并。因此,DO操作符将没有要记录的数据。所以,如果你看到我的解决方案,我就补救了。@AndrewLobban你是对的,对此我很抱歉。我应该用switchMap而不是map。我已经更新了答案。您可以在switchMap之后进行映射,并检查那里的响应是否为null