我有一个服务,它返回一个 Observable
,我在该服务上映射以解析响应,并使用一个 catch 子句来捕获错误(如果有)。但问题是我需要更新处理程序方法中的数据成员,并将其共享给其他组件使用。
import { stuff } from 'stuffs';
class AppService() {
private errorMsg = new Subject<string>();
// ... More methods here
private handleError(error) {
// Problem - `this` here doesn't point to AppService
this.errorMsg.next('Sharable error message to component!');
return Observable.throw(error.message);
}
callLoginApi(userDetails: {email: string, password: string}):
Observable<any> {
return this.http.post(this.postURI, userDetails)
.map(this.extractData)
.catch(this.handleError);
}
}
最佳答案
.catch(this.handleError.bind(this));
或
.catch(err => this.handleError(err));
关于javascript - 从 catch 子句访问类成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318670/