我在Angular 8中创建了一个项目,包括ngx-translate(https://github.com/ngx-translate/core)和使用Auth0(https://auth0.com/docs/quickstart/spa/angular2/01-login#install-the-sdk)的登录。他们分开工作很好,但是当我在一起时我有一个奇怪的错误。
在控制台的项目中,对于HTML的每个翻译文本,都会收到此错误。
core.js:6014 ERROR
{error: "login_required", error_description: "Login required", state: "aGNHczdqZTRjMzFTM3ZtWnpIOS1kQjdXaXV2dktyVW94ZkdsYm5KVGhRSA=="}
error: "login_required"
error_description: "Login required"
state: "aGNHczdqZTRjMzFTM3ZtWnpIOS1kQjdXaXV2dktyVW94ZkdsYm5KVGhRSA=="
__proto__: Object
当我在拦截器的app.module.ts中注释提供程序时,我将令牌添加到后端中,要求翻译工作正常,一切都很好。但随后,我的令牌将不会发送到后端以进行我的请求。
providers: [
// {
// provide: HTTP_INTERCEPTORS,
// useClass: InterceptorService,
// multi: true
// }
]
当我在AppComponent中注释默认语言时,没有更多错误,令牌将在我的请求中发送到后端。但是后来我看不到前端的文本,因为它没有翻译。
export class AppComponent {
constructor(public translate: TranslateService) {
// translate.setDefaultLang('en');
}
}
起初,该错误似乎表明我尚未登录,但登录后会执行相同的操作。
这是我的拦截器类的代码
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { mergeMap, catchError } from 'rxjs/operators';
import { AuthService } from './authentication/auth.service';
@Injectable({
providedIn: 'root'
})
export class InterceptorService implements HttpInterceptor {
constructor(private authService: AuthService) { }
public intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return this.authService.getTokenSilently$().pipe(
mergeMap(token => {
const tokenReq = req.clone({
setHeaders: { Authorization: `Bearer ${token}` }
});
return next.handle(tokenReq);
}),
catchError(err => throwError(err))
);
}
}
谢谢
答案 0 :(得分:0)
我与Auth0社区合作。这听起来与Auth0没有直接关系,因为听起来您已成功确认状态/会话。我不是100%熟悉NGX Translate,但是我确实在这方面找到了可以帮助您的教程。很抱歉,我无法在这方面提供更多见解。
https://www.codeandweb.com/babeledit/tutorials/how-to-translate-your-angular8-app-with-ngx-translate