我正在使用 ngx-translate,现在我想添加一个 LoadingInterceptor。因此,在发出 API 请求时显示 LoadingSpinner 的 HTTPInterceptor。
我已经在使用令牌拦截器。但是当我安装 LoadingInterceptor 时,ngx-translate 不再起作用。
这是我的 loading.interceptor.ts
import { Observable } from 'rxjs';
// import { LoadingService } from './../../../services/loading.service';
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
@Injectable()
export class LoadingInterceptor implements HttpInterceptor {
// Liste von Requests
private requests: HttpRequest<any>[] = [];
constructor() {}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('detect request', request.url);
return new Observable(observer => {
});
}
}
这是我的 app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
// Translate
import { TranslateModule, TranslateLoader, TranslateService } from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
// Interceptors
import { HTTP_INTERCEPTORS, HttpClient, HttpClientModule } from '@angular/common/http';
import { TokenInterceptor } from './interceptors/token.interceptor';
import { LoadingInterceptor } from './interceptors/loading.interceptor';
// tslint:disable-next-line:typedef
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
TranslateModule.forRoot({
defaultLanguage: 'de',
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
providers: [
[
{
provide: HTTP_INTERCEPTORS,
useClass: LoadingInterceptor,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}
],
TranslateService,
],
bootstrap: [AppComponent]
})
export class AppModule { }
这是我的 html 标记,其中包含要翻译的变量:
<span>{{ 'TEST' | translate }}</span>
这是我 i18n 文件夹中的 de.json 文件:
{
"TEST": "Hello world"
}
我没有收到错误消息,只是不再显示要翻译的变量。除非我注释掉 app.module.ts 中的 LoadingInterceptor。