在 Angular2 服务中取消订阅 http 订阅的最佳做法是什么?
目前我这样做,但我不确定这是否是最好的方法。
import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import { Subject } from "rxjs/Subject";
import { ISubscription } from "rxjs/Subscription";
@Injectable()
export class SearchService {
private _searchSource = new Subject<any>();
public search$ = this._searchSource.asObservable();
constructor(private _http: Http) {}
public search(value: string) {
let sub: ISubscription = this._http.get("/api/search?value=" + value)
.map(response => <any>response.json())
.do(data => this._searchSource.next(data))
.finally(() => sub.unsubscribe()).subscribe();
}
}