【问题标题】:ngx-translate not working after i add HttpInterceptor添加 HttpInterceptor 后,ngx-translate 无法正常工作
【发布时间】:2021-10-27 06:59:26
【问题描述】:

正如标题所说,在我将 HttpInterceptor 添加到我的代码之前一切正常,问题是我有两种方法来翻译我的字段,一种仍然可以正常工作,第二种不像下面显示的那样

 <dx-button 
    (click)="logIn()" 
    type="default" id="buttonLogIn"
    text="{{ 'LoginPage.Login'|translate }}">
</dx-button>
<span id="containerStayConnected"><input type="checkbox" id="stayConnected" [(ngModel)]="stayConnected" /><label for="stayConnected" translate>LoginPage.StayConnected</label></span>
<a id="forgetPassword" translate (click)="isPopupForgotPasswordVisible = !isPopupForgotPasswordVisible">LoginPage.ForgotPassword</a><br><br><br>

应答器内的翻译正在工作,而在 dxButton 中使用管道的翻译不起作用 这是我的拦截器服务

@Injectable()
export class AuthInterceptorService implements HttpInterceptor {

constructor(public auth: AuthService) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const userId = localStorage.getItem('access_token');
    request = request.clone({
        setHeaders: {
            Authorization: `Bearer ${userId}`
        }
    });
    return next.handle(request);
}

} 有没有人知道为什么在我添加 httpInterceptor 之后使用 Pipe 停止工作的翻译。

【问题讨论】:

    标签: angular6 angular-http-interceptors angular-pipe angular-i18n


    【解决方案1】:

    我通过从构造函数中删除 public auth: AuthService 解决了这个问题。 我认为这里造成问题的是 HttpInterceptor 和 AuthService 都注入了​​ HttpClient。 如果有人有兴趣查看有关类似问题的更多信息,请查看此处 https://github.com/angular/angular/issues/18224

    【讨论】:

    • 是的,存在循环依赖,删除循环依赖后它正在工作
    • 我有同样的问题,我浏览了你提到的链接,但它仍然不适合我。我只使用 HttpClient。除了一个简单的拦截器,我没有任何身份验证服务。我在这里添加了我的拦截器代码codepen.io/rajeshwarpatlolla/pen/rNeLOQj,我还在底部的同一个文件中添加了翻译加载器代码。请帮我解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多