Angular 如何在导出函数中禁用拦截器?

Angular 如何在导出函数中禁用拦截器?,angular,localization,interceptor,angular-http-interceptors,ngx-translate,Angular,Localization,Interceptor,Angular Http Interceptors,Ngx Translate,我使用ngx翻译进行本地化。当我试图读取存储在本地文件夹中的本地化json文件时,收到一个错误HttpErrorResponse{headers:HttpHeaders,status:404,statusText:“Not Found”,url:“http:/host.com/api/assets/i18n/en.json”,ok:false,}。我想问题出在我应用程序中使用的拦截器上 // app.module.ts export function HttpLoaderFactory(http

我使用ngx翻译进行本地化。当我试图读取存储在本地文件夹中的本地化json文件时,收到一个错误HttpErrorResponse{headers:HttpHeaders,status:404,statusText:“Not Found”,url:“http:/host.com/api/assets/i18n/en.json”,ok:false,}。我想问题出在我应用程序中使用的拦截器上

// app.module.ts
export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http, 'assets/i18n/', '.json');
}
如何在此函数中禁用拦截器?我试图将header设置为X-Skip-Interceptor,但没有帮助。

//如下所示读取本地json数据
//Read your local json data like below

private _productURL = 'api/products/products.json';    
getProducts(): Observable<any> {
        return this._http.get(this._productURL).map((response : Response) => <any> response.json())
        .do(data =>console.log(JSON.stringify(data))).catch(this.handleError);
}
private_productURL='api/products/products.json'; getProducts():可观察{ 返回此。_http.get(this._productURL).map((response:response)=>response.json()) .do(data=>console.log(JSON.stringify(data))).catch(this.handleError); }
您可以使用HttpBackend

注入时,HttpBackend将请求直接发送到 后端,无需通过拦截器链

在app.module.ts中,将HttpBackend作为deps而不是HttpClient传递

TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (HttpLoaderFactory),
        deps: [HttpBackend],
      },
    }),
并对HttpLoaderFactory进行如下修改

export function HttpLoaderFactory(handler: HttpBackend) {

    const http = new HttpClient(handler);

    return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

有一个简单的解决办法。您可以如上所述跳过翻译文件

function MSALAngularConfigFactory(): MsalAngularConfiguration {
  return {
    popUp: false,
    consentScopes: [
      environment.backendConfig.resourceScope
    ],
    protectedResourceMap: [
      [environment.backendConfig.url, [environment.backendConfig.resourceScope]],
      ['./assets/i18n/*.json', null]
    ],
  };
}
我们在
protectedResourceMap
中设置了此选项。如果您得到的错误为“src/app/app.module.ts:47:30中的错误-错误TS2322:Type'null'不可分配给Type'string[]”。请将
用作任何

protectedResourceMap: [
      [environment.backendConfig.url, [environment.backendConfig.resourceScope]],
      ['./assets/i18n/*.json', null as any]
    ]
如果你的图片没有加载到你的网站上,这可能是你的CSS的一个问题。将
background
样式从
background:url(../../../assets/img/background.svg)
更改为
background:url(/assets/img/background.svg)
。你可以在这本书里读到更多