5
私のAngular2-typescriptアプリでは、forkJoinを使用して、すべての並列HTTP呼び出しが行われた後にObservableを返すだけです。forkJoinから返されたObservableの登録を解除するには?
問題:サブスクリプションのコールバックは無期限にここで
を実行され続けては、私のコードです:
http.service
import {Http} from "@angular/http";
constructor (private _http: HTTP) {}
makeGetRequest(....) {
return this._http.get(URL)
.map (res => res.json)
.toPromise();
my.service
import {Observable} from "rxjs/Observable";
import {HttpService} from "http.service"
constructor (private _httpService: HttpService) {}
myMethod(): Observable<any[]> {
return Observable.forkJoin(
this._httpService.makeGetRequest(
URL1
),
this._httpService.makeGetRequest(
URL2
)
)
}
私は(同じ問題)しようとした何
my.component
import MyService from "my.service";
import Subscription from "rxjs";
constructor (private _service: MyService) {}
mySub: Subscription;
ngOnInit() {
this.mySub = this._service.myMethod.subscribe(data => {
data.forEach(console.log(data));
this.mySub.unsubscribe();
}
}
:むしろmy.component使用中の約束
- リターンを1次回()。subscribe()の代わりに.subscribe()
- this.mySub.unsubscribe();を入れてください。 ngOnInitの終了時ではなく(またsetTimeoutを有する(()=> ...))サブスクライブコールバック
「サブスクリプションコールバックは無期限に実行され続けます」という意味はどうですか? – Picci
質問は理にかなっていません。 forkJoinは* completed * observablesの結果を結合します.1つの値を持つので、unsubscribedしないでください。無期限に実行されている*に問題がある場合は、あなたの問題を複製できる[MCVE](http://stackoverflow.com/help/mcve)を – estus
にお寄せください。明らかに、サブスクリプションが2回インスタンス化され、問題が発生したため、これが発生していました。私はそれを受け入れるでしょう、質問に答えること自由に感じてください;) – dragonmnl