當我有一個Angular service類(WeatherService),它對外部api執行REST調用並在searchWeather中提取數據時, )功能。Angular 2 Subject.next一旦Observable的監聽器拋出一個錯誤就不會工作
我有一個組件WeatherSearchComponent,它的模板中有一個輸入字段搜索,它具有綁定到組件中函數onSearch()的鍵名事件。它基本上是一個TypeAhead輸入字段。我在類和onSearch()函數中定義了一個主題,我正在初始化主題的next()函數,如下所示。
WeatherSearchComponent:
private searchStream = new Subject<string>();
onSearch(cityName:string) {
this.searchStream
.next(cityName);
}
constructor(private _weatherService:WeatherService) {
}
ngOnInit() {
this.searchStream
.debounceTime(300)
.distinctUntilChanged()
.switchMap((input:string) => this._weatherService.searchWeatherData(input))
.subscribe(
data => this.data = data
);
}
的WeatherService:
searchWeatherData(cityName: string): Observable<any> {
return this._http.get('URL')
.map(response => response.json())
.catch(error => {
console.error(error);
return Observable.throw(error.json())
});
}
我面對的是,如果REST調用失敗或返回一個錯誤,IM登入它的問題,但主題searchStream生命週期一旦出現錯誤並且在此之後不接受輸入就結束了。我試着在ngOnInit()方法中處理錯誤,但它不起作用。
我想知道如何處理這種情況?
是的,上面的實現工作。我想知道該實施是否適合在生產環境中使用?或者我應該有什麼不同? –