2017-01-25 1 views
1

여러 개의 HTTP 요청을 하나의 관찰 가능 항목으로 포크하고 간격으로 호출하고 모든 구독자간에 동일한 데이터를 공유하려고합니다.

내 코드는 지금까지 다음과 같습니다

import {Observable} from "rxjs/Rx"; 

let obs = Observable 
    .interval(10000) 
    .publish() 
    .connect() 
    .forkJoin(
     this.http.get(API_URL + "x", {headers: this.headers}) 
     .map(response => response.json()), 
     this.http.get(API_URL + "y", {headers: this.headers}) 
     .map(response => response.json()) 
    ) 
    .map(res => { 
    // do some stuff 
    return res; 
    }); 

오류 : 내가 읽은

Typescript Error 
Property 'forkJoin' does not exist on type 'Subscription'. 

:

https://blog.thoughtram.io/angular/2016/06/16/cold-vs-hot-observables.html

Multiple $http requests in Ionic2

http://restlet.com/blog/2016/04/12/interacting-efficiently-with-a-restful-service-with-angular2-and-rxjs-part-2/

고마워요!

+0

'publish()'및'connect()'없이 시도 했습니까? – chrigu

+0

이 오류의 결과 : 'forkJoin'속성이 'Observable '유형에 없습니다. – kaynow

+0

적어도 Subscription 대신 Observable이 있어야합니다. 어쩌면 당신은 http://stackoverflow.com/questions/38443798/property-forkjoin-does-not-exist-on-type-typeof-observable-angular2 – chrigu

답변

2

이런 식으로 뭔가 작업을해야합니다 : 다음

let source = Observable 
    .interval(1000) 
    .flatMap(() => { 
     return Rx.Observable.forkJoin(
      this.http.get(API_URL + "x", {headers: this.headers}) 
       .map(response => response.json()), 
      this.http.get(API_URL + "y", {headers: this.headers}) 
       .map(response => response.json()) 
     ) 
    }) 
    .publish(); 

source.connect(); 

관측이 관찰

source.subscribe(...); 

내가 여기서 뭘하고있어 간격 관찰을 가지고의 forkJoin에 대한 각 값을 대체하는 것입니다 가입 모든 작업. 그런 다음 동일한 데이터를 여러 구독에 공유하도록 게시합니다.

구현시에 또 다른 문제는 .connect()에서 구독을 반환한다는 것입니다. 이는 구독 할 때마다 구독을 취소하기위한 것입니다. 참관인은 출간 된 출처에 가입해야합니다.

+0

감사합니다! 작동 중! 필자의 경우 "Rx.Observable.forkJoin"을 "Observable.forkJoin"으로 대체해야했습니다. 좋은 설명 주셔서 감사합니다;) – kaynow