角度6:句柄错误无法读取注入的服务?



我似乎无法读取handleError函数中注入的服务

constructor(private http: HttpClient, public _translate: TranslateService) { }        
login(user: User): Observable<User> {               
console.log( this._translate); // I CAN READ THE SERVICE INJECTED    
return this.http.post<User>("http://...../login",  params, httpOptions)
.pipe(catchError(this.handleError));                  
}
private handleError(error: HttpErrorResponse) {    
console.log( this._translate); I CAN'T READ THE SERVICE INJECTED                           
return throwError(error.error);                     
};

使用未绑定函数作为.pipe(catchError(this.handleError));中的参数会丢失this上下文

应该是

.pipe(catchError(this.handleError.bind(this)));

或使用箭头功能

.pipe(catchError((err)=>this.handleError(err)));    

相关内容

最新更新