如何取消订阅forkJoin返回的Observable?

dra*_*mnl 8 fork-join rxjs angular

在我的Angular2-typescript应用程序中,我只使用forkJoin在所有并行HTTP调用完成后返回一个Observable.

问题:订阅回调一直无限期地执行

这是我的代码:

http.service

import {Http} from "@angular/http";

constructor (private _http: HTTP) {}

makeGetRequest(....) {
    return this._http.get(URL)
           .map (res => res.json)
           .toPromise();
Run Code Online (Sandbox Code Playgroud)

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
            )
        )
}
Run Code Online (Sandbox Code Playgroud)

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();
     }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过(同样的问题):

  • 在Http.service中返回一个Observable而不是Promise
  • 在my.component中使用.first().subscribe()而不是subscribe()
  • 把this.mySub.unsubscribe(); 在ngOnInit的末尾而不是在subscribe回调内部(也使用setTimeout(()=> ....))

Est*_*ask 22

forkJoin参考所述,它

并行运行所有可观察序列并收集它们的最后元素.

这意味着运算符从已完成的observable中获取值,并返回具有单个值的已完成的observable.没有必要取消订阅.