Angular2 - Error: cannot resolve all parameters for IconService

I try to switch my application to AoT compilation and get this error in the working environment when the application loads (it works fine locally).

Error: Can't resolve all parameters for IconService: (?, ?)

it seems that the error comes from the modules providing the IconService. Icon Services Designer looks like

constructor(private http:Http, private iconConfiguror:IconConfiguror) {

So, my question is what this error means and why it will only happen in the prod environment (I tried to enable local prod mode)?

This seems to mean that no http and icon configuration parameters are specified, but the icon configuration is provided at the application module level, and the HttpModule imported into IconModule , where the IconService provided.

 @NgModule({ imports: [ CommonModule, HttpModule, ], declarations: [ IconComponent, ], exports: [ IconComponent, ], providers: [ IconService, __platform_browser_private__.BROWSER_SANITIZATION_PROVIDERS, ], }) 

And a barrel for our icon component.

 export * from "./components/icon/icon.configuror"; export * from "./components/icon/icon.service.provider"; export * from "./components/icon/icon.service"; export * from "./components/icon/icon.component"; export * from "./components/icon/icon.module"; 
+5
source share
6 answers

This is fixed by providing IconService in a different way.

  { provide: IconService, useFactory: iconServiceFactory, deps: [Http, IconConfiguror], }, 

and the factory itself

 export function iconServiceFactory(http: Http, iconConfiguror: IconConfiguror) { return new IconService(http, iconConfiguror); } 

I think for some reason Http was not provided (although the HttpModule was imported), so I had to declare it as a dependency.

+5
source

One possible reason for this error is that you are not decorating the IconService @Injectable() class. If this is the reason, adding that the decoration above the class declaration will fix the error.

+5
source

I ran into a similar problem. I solved this by changing the export order in the trunk.

Main service files:

 // dependency.service.ts @Injectable() export class DependencyService { } // dependant.service.ts import { DependencyService } from '.'; @Injectable() export class DependantService { constructor(private dependency: DependencyService) { } } 

The following barrel causes an error:

 // index.ts export * from './dependant.service'; export * from './dependency.service'; 

When performing the following work:

 // index.ts export * from './dependency.service'; export * from './dependant.service'; 
+2
source

Sometimes, the only way to fix it is to manually describe the parameters.

 static get parameters() { return [Http, IconConfiguror] } constructor(private http:Http, private iconConfiguror:IconConfiguror) { 
+1
source

Worked for me when I used the following command when importing a service in app.module.ts

 {provide: AuthService, depends: HttpClientModule} 
0
source

My problem was that I inherited the base class and I assigned this base class @Injectable. The inheritance class was a class that should have the @Injectable attribute, not the base class. It appears that when the compiler sees @Injectable attibute, it checks that all properties in the constructor can be entered. If not, this is a mistake. I solved this by removing @Injectable attibute from this class.

0
source

Source: https://habr.com/ru/post/1264646/


All Articles