角度翻译服务在另一个服务中未定义

问题描述

我想在另一个服务中使用translate服务,但是translate服务未定义。

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);
    }
}

当API返回错误时,此行应执行:

errorMessage = this.translate.instant("SHARED.LOGIN_ERROR");

但我收到此错误TypeError: Cannot read property 'instant' of undefined

请问有人可以建议如何使用translate服务在handleError函数内部进行翻译,为什么会出现此错误?谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)