Angular2: One instance of module in multiple modules

天涯浪子 提交于 2019-12-11 05:54:20

问题


Let's make example for ng2-translate plugin.

I have root AppModule, and children TopPanelModule and PagesModule.


I configure ng2-translate for AppModule.

@NgModule({
  imports: [TranslateModule.forRoot({
    provide: TranslateLoader,
    useFactory: (http: Http) => new TranslateStaticLoader(http, '/assets/i18n', '.json'),
    deps: [Http]
  })],
  exports: []
})
export class AppModule {
  constructor(translateService: TranslateService) {    
  }
}

In AppComponent I set languages and basic configuration for TranslateModule.

@Component(...)
export class AppComponent {
  constructor(translateService: TranslateService) {
    translateService.addLangs(["en", "fr"]);
    translateService.setDefaultLang('fr');

    const browserLang = 'fr';
    translateService.use(browserLang.match(/en|fr/) ? browserLang : 'en');    
  }
}

And then I'm trying to use TranslateModule in children modules components - TopPanelComponent, PagesComponent. It does'n work. Pipe not exists error, no translate etc..


For resolving this problem, I create a separate module MyTranslateModule, configure TranslateModule on it, and use this module in my PagesModule and TopPanelModule.

@NgModule({
  imports: [TranslateModule.forRoot({
    provide: TranslateLoader,
    useFactory: (http: Http) => new TranslateStaticLoader(http, '/assets/i18n', '.json'),
    deps: [Http]
  })],
  exports: [TranslateModule]
})
export class MyTranslateModule {
  constructor(translateService: TranslateService) {
    translateService.addLangs(["en", "fr"]);
    translateService.setDefaultLang('fr');

    const browserLang = 'fr';
    translateService.use(browserLang.match(/en|fr/) ? browserLang : 'en');

    console.log('AdminTrModule: calling');
  }
}

and

@NgModule({
  imports: [MyTranslateModule]
})
export class PagesModule{

}

@NgModule({
  imports: [MyTranslateModule]
})
export class TopPanelModule{

}

This is important part. It works! But I think it creates a two instances of TranslateModule, so, when I change transalte langauge by calling in TopComponent translateService.use('en'), it change language in TopPanelModule, but not in PagesModule.


回答1:


You need to define a forRoot function in module 'MyTranslateModule'

export class MyTranslateModule {

static forRoot(): ModuleWithProviders {
    return {
      ngModule: MyTranslateModule ,
      providers: [TranslateService,TRANSLATION_PROVIDERS]
    };
  }

}

then import MyTranslateModule in appModule as following

@NgModule({
  bootstrap: [App],
  declarations: [
    App
  ],
  imports: [ // import Angular's modules
    BrowserModule,
    MyTranslateModule.forRoot(),
    PagesModule,
    TopPanelModule
  ],
  providers: [ // expose our Services and Providers into Angular's dependency ]
})

in this case translate service will be created as singleton "one instance along the application"



来源:https://stackoverflow.com/questions/41243958/angular2-one-instance-of-module-in-multiple-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!