0

I have a service called routing.service which subscribes to routing event and when a parameter changes, it updates Translate service. As following in constructor:

this.router.events.subscribe(val => {
    if (val instanceof ActivationEnd ) {
        let myVal = val.snapshot.params.lang;
        this.currentLang = myVal;
        this.translate.use(myVal);
        this.translate.setDefaultLang(config.defaultLang);
    }
});

I have a shared module that is imported into the app module. In shared component everything works fine:

<div [innerHTML]="'HOME.TITLE' | translate"></div>

But in my lazy loaded modules not working. Even I can't access to this.currentLang that is inside subscribe. Any idea?

Update: Here is more detail about my code:

app.module:

imports:[
  CommonModule,
  BrowserModule,
  FormsModule,
  NgtUniversalModule,
  TransferHttpCacheModule,
  HttpClientModule,
  AppRoutingModule,
  SharedModule
],

shared.module:

imports: [
  CommonModule,
  RouterModule,
  TranslateModule.forRoot({
    loader: {
      provide: TranslateLoader,
      useClass: TranslateUniversalLoader,
    }
  })
],
exports: [
  HeaderComponent,
  TranslateModule
]

lazy-loaded.module:

imports: [
  CommonModule,
  FormsModule,
  CustomersRoutingModule,
  SharedModule,
],

My app.component:

<!-- This is component of my shared.module and translation works fine here, but not in lazy loaded modules -->
<saz-header></saz-header> 
<router-outlet></router-outlet>
7
  • I have a feeling you may have to import SharedModule into your other modules if you want to use your router.service. The lazy loaded modules wont be able to access it unless they import your SharedModule. Personally, I'd recommend moving your routing.service possibly into your AppModule. It makes more sense to have it there, or to a CoreModule Commented Mar 28, 2019 at 7:23
  • @N15M0_jk I have already imported SharedModule into my other lazy module. And what do you mean by move your routing.service possibly into your AppModule Commented Mar 28, 2019 at 7:31
  • Hmmm, that's unusual then. When I mentioned move your routing.service possibly into your AppModule, I assumed your routing.service isn't a provider in your AppModule. Would you kindly share your AppModule, SharedModule and lazy loaded module files so we can see how everything is imported/etc? Commented Mar 28, 2019 at 7:44
  • Just updated the post. Please see Update part. Thanks. Commented Mar 28, 2019 at 7:57
  • 1
    Ops, I have forgotten to inject the service in my SharedModule component. Now it works fine. Thanks. Commented Mar 28, 2019 at 8:16

2 Answers 2

1

Glad you got it working, but for sake of completion:

1) Double check in which module your service is provided. Generally, a routing service should always be accessible, and it makes sense to load with your AppModule, or depending on if you follow John Papa's suggested project structure, a CoreModule.

In your case, it might be logical to have the service imported in the AppRoutingModule.

2) If you do have a service that you'd like lazy loaded, it might be beneficial to include said service in an isolated, simple module that you can lazy load whenever you need it to (simply put, a small Module that just includes the few things you may or may not need)

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for your answer. But when I provide the service in AppModule, still in lazy-loaded module I can't access to service. I must provide it in SharedModule.
Cool, will check it out soon!
Here you go, sorry about that: stackblitz.com/edit/angular-no-for-root-fz46kh
Thank you so much. And one more question. Should I import my external module (in this case TranslateModule) in AppModule or SharedModule? (I use TranslateService in my own routing.service)
|
0

I had just forgotten to add the provider to my SharedModule as following:

providers: [
  RoutingService
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.