I want to use the translate
service inside another service, but the translate
service is undefined.
import { TranslateService } from '@ngx-translate/core';
@Injectable({
providedIn: 'root'
})
export class UsersService {
constructor(private translate: TranslateService) {
// this.translate is available here
}
loginUser(user: UserLogin): Observable<User> {
return this.http.post<User>(this.signInUrl, user)
.pipe(catchError(this.handleError));
}
handleError(err) {
// this.translate is NOT available here
let errorMessage = this.translate.instant("SHARED.LOGIN_ERROR"); // This line gives the error
return throwError(errorMessage);
}
}
When the API returns an error, this line should execute:
errorMessage = this.translate.instant("SHARED.LOGIN_ERROR");
but I get this error: TypeError: Cannot read property 'instant' of undefined
Can someone please advise how can use the translate
service to translate something inside the handleError
function and why do I get this error? Thanks!