scttcper / ngx-toastr

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

Getting error: StaticInjectorError(Platform: core)[InjectionToken ToastConfig] in angular 8 #726

Open sonusindhu opened 5 years ago

sonusindhu commented 5 years ago

Hi Guyz,

Thank you for such a great package.

I have ngx-toastr package installed on my project but I am not using it right now. so I have no export that module in my app component. Once I run the application I am getting this error:

core.js:4002 ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[InjectionToken ToastConfig]: StaticInjectorError(Platform: core)[InjectionToken ToastConfig]: NullInjectorError: No provider for InjectionToken ToastConfig! NullInjectorError: StaticInjectorError(AppModule)[InjectionToken ToastConfig]: StaticInjectorError(Platform: core)[InjectionToken ToastConfig]:

Once I import:

import { ToastrModule } from 'ngx-toastr';

ToastrModule.forRoot({
      timeOut: 5000,
      positionClass: 'toast-bottom-center',
      preventDuplicates: true,
      closeButton: true,
      progressBar: true,
      maxOpened: 1,
      autoDismiss: true,
      enableHtml: true
    }), 

then that is fine. As I am planning to use in the future. Do I need to import it?

scttcper commented 5 years ago

What?

shayaneumar-mitel commented 4 years ago

@sonusindhu Are you importing it in the App Module?

@scttcper I have observed that when I import the module in any lazy loaded module I am getting the same error that @sonusindhu has mentioned. Importing it in the app module works fine.

Does the import work only in AppModule?

custompro12 commented 4 years ago

I can confirm that I get the same error when attempting to import the ToastrModule inside of a lazy loaded feature module.

scttcper commented 4 years ago

Yeah I would assume this module has problems being lazy loaded because of the “entryCompnents”

hallowatcher commented 4 years ago

is there any workarounds for this? this is a critical bug atm and I cannot use toasts at all inside lazy loaded modules

also note that this only happens after an ng build (at least for me)

scttcper commented 4 years ago

does anyone have a small reproduction?

ng-model commented 4 years ago

yes the import will only work in app.module and not in any lazy loaded modules.

heinrichcoetzee commented 4 years ago

Been almost a Year and seems this issue still exists...

scttcper commented 4 years ago

@Vaaljan whats the fix?

heinrichcoetzee commented 4 years ago

@scttcper I think the problem has to do with the version of nxg-toastr. I was using 12.1.0 with Angular 8 which seems incompatible. So I downgraded to 11.3.3 on Angular 8

Also had to make sure that the import into the lazy module only happens once like in the AppModule.