2

I am getting the following exception in angular 2.

EXCEPTION: Uncaught (in promise): Error: No provider for SettingsService!
Error: DI Error
    at NoProviderError.BaseError [as constructor] (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:1244:31) [angular]
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:1365:20) [angular]
    at new NoProviderError (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:1405:20) [angular]
    at ReflectiveInjector_._throwOrNull (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:2937:23) [angular]
    at ReflectiveInjector_._getByKeyDefault (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:2976:29) [angular]
    at ReflectiveInjector_._getByKey (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:2908:29) [angular]
    at ReflectiveInjector_.get (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:2777:25) [angular]
    at AppModuleInjector.get (/AppModule/module.ngfactory.js:483:165) [angular]
    at AppModuleInjector.getInternal (/AppModule/module.ngfactory.js:621:49) [angular]
    at AppModuleInjector.NgModuleInjector.get (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:8490:48) [angular]
    at PreActivation.getToken (http://localhost:5555/node_modules/@angular/router/bundles/router.umd.js:4160:29) [angular]
    at MapSubscriber.eval [as project] (http://localhost:5555/node_modules/@angular/router/bundles/router.umd.js:4056:52) [angular]
    at MapSubscriber._next (http://localhost:5555/node_modules/.tmp/Rx.min.js?1488555021058:11:17343) [angular]
    at MapSubscriber.Subscriber.next (http://localhost:5555/node_modules/.tmp/Rx.min.js?1488555021058:11:13507) [angular]

The issue is related to angular 2 DI setup via the provider is unable to resolve the SettingsService that is being used in an Authentication Guard class.

AppModule has the following setup in its provider

...
providers: [
Service1,
Service2,
KCService,
SettingsService,
AuthenticationGuard,
{
  provide: Http,
  useFactory:
  (backend: XHRBackend,
    defaultOptions: RequestOptions,
    kcService:KCService) => new KCHttp(backend, defaultOptions, kcService),
  deps: [XHRBackend, RequestOptions, KCService]
},
{
  provide: APP_BASE_HREF,
  useValue: '<%= APP_BASE %>'
}],
bootstrap: [AppComponent]
....

And the Authentication Guard has the following

...
@Injectable()
export class AuthenticationGuard implements CanActivate {

constructor(
private router: Router,
private kcService: KCService,
private http: Http,
private settingsService: SettingsService
) {
}

canActivate(route: ActivatedRouteSnapshot) {
...

The error is occurring when AppModule is trying to create AuthenticationGuard and is unable to resolve or create SettingsService via DI. Is there a better way to setup the DI or better way to debug this issue?

simplicity
  • 31
  • 4

1 Answers1

0

Remove the unwanted constructor parameter from below code

constructor(
private router: Router,
private kcService: KCService,
private http: Http,
private settingsService: SettingsService
) {
}
User12345
  • 5,180
  • 14
  • 58
  • 105