在角教程中,我有這個爲什麼在角度教程中,我可以做'this.handleError'沒有通過參數?
private heroesUrl = 'api/heroes'; // URL to web api
constructor(private http: Http) { }
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
我的問題是,如何.catch(this.handleError)
作品?
爲什麼我可以做this.handleError
沒有通過參數?
其所謂的回調函數。 –
根據文檔(reactivex.io/rxjs/class/es6/...),'catch'方法將函數作爲參數。這個函數需要2個參數(錯誤,被捕獲)。我將'handleError'函數傳遞給「catch」,我的handleError只接收1個參數(錯誤)。如果我將handleError函數更改爲「handleError(error,caught)」,仍然有效。這怎麼可能? –