scttcper / ngx-toastr

🍞 Angular Toastr
https://ngx-toastr.vercel.app
MIT License
2.52k stars 358 forks source link

shared module issues #651

Open ajazhmd opened 5 years ago

ajazhmd commented 5 years ago

I am using ngx-toastr in one of my angular 7 library project, it is been consumed by another host project. The host project works perfectly fine in dev version, but when i create production build(using ng build --prod) it gives me following error:


StaticInjectorError(Platform: core)[InjectionToken ToastConfig]: 
NullInjectorError: No provider for InjectionToken ToastConfig!
Error: StaticInjectorError[InjectionToken ToastConfig]: 
StaticInjectorError(Platform: core)[InjectionToken ToastConfig]: 
NullInjectorError: No provider for InjectionToken ToastConfig!```

I am already importing  **ToastrModule.forRoot()** in my library module, not sure why there's dual behavior for dev and prod environment? 
scttcper commented 5 years ago

there isn't a way to import toastr from a shared module right now.

RoRoche commented 4 years ago

I'm facing the same issue. No current workaround?

ajazhmd commented 4 years ago

@RoRoche I had to include ToastrModule.forRoot() in the imports of app module, it worked for me.

RoRoche commented 4 years ago

@ajazhmd here is the code I use:

@NgModule({
  declarations: [AppComponent],
  imports: [
    ...
    ToastrModule.forRoot()
  ],
})
export class AppModule {}

@NgModule({
  imports: [
    ...
    ToastrModule
  ],
  declarations: [OneSharedComponent, AnotherSharedComponent]
})
export class OneSharedModule {
}

@NgModule({
  imports: [
    ...
    ToastrModule
  ],
  declarations: [SecondSharedComponent, StillAnotherSharedComponent]
})
export class SecondSharedModule {
}

I run using the following command:

ng serve core-app --prod

But when I go to the screen, I'm still facing this error:

ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[OneSharedComponent -> ToastrService]: 
  StaticInjectorError(Platform: core)[OneSharedComponent -> ToastrService]: 
    NullInjectorError: No provider for ToastrService!

Am I missing something? Is my configuration wrong?

P.S. : what is weird is when I run ng serve core-app (i.e., without the --prod flag), all works fine. P.S. : please excuse me for asking stupid questions, I'm not used to Angular 8 development and started few weeks ago on an existing project based on Nx.

ajazhmd commented 4 years ago

@RoRoche try importing ToastrModule.forRoot() instead ToastrModule in your shared modules.