StaticInjectorError(平台:核心)[HttpHandler->注入器]:NullInjectorError:没有注入器提供程序

时间:2019-05-08 11:44:48

标签: angular7 ionic4

我创建了一个共享库,并使用ngx-translate提供了翻译服务。以下是共享库的我的module.ts文件。

import { NgModule, ModuleWithProviders } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicModule } from 'ionic-angular'; 
import { Utils } from '.';
import { NetworkService } from './network-service';
import { Network } from '@ionic-native/network/ngx';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader, TranslateService, TranslateStore } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { TranslationService } from './translate-service';
import { HttpModule } from '@angular/http';
import { CommonModule } from '@angular/common';


export function initTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
}

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    TranslateModule.forChild({
      loader: {
        provide: TranslateLoader,
        useFactory: initTranslateLoader,
        deps: [HttpClient]
      }
    })
  ],
  declarations: [],
  exports: [TranslateModule,HttpClientModule],
  providers: [ Network,NetworkService,TranslationService, TranslateStore ]
})
export class UtilsModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: UtilsModule,
      providers: [Network,NetworkService,TranslationService,TranslateStore ]
    }
  }
}

在我的应用中使用上述模块时,出现以下错误:

ERROR Error: StaticInjectorError(AppModule)[HttpHandler -> Injector]: 
  StaticInjectorError(Platform: core)[HttpHandler -> Injector]: 
    NullInjectorError: No provider for Injector!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:8896)
    at resolveToken (core.js:9141)
    at tryResolveToken (core.js:9085)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8982)
    at resolveToken (core.js:9141)
    at tryResolveToken (core.js:9085)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8982)
    at resolveNgModuleDep (core.js:21218)
    at _createClass (core.js:21265)
    at _createProviderInstance (core.js:21235)

我该如何解决?如果我删除所有与翻译相关的代码,一切都会正常。

0 个答案:

没有答案