5

I've been trying to switch my app over to AoT compilation and have been getting this error in the production environment when the app is loading (it works fine locally).

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

it seems like the error is coming from on the modules that is providing the IconService. The icons services constructor looks like

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

So my question is what does this error mean and why would it happen in the prod environment only (I've tried enabling prod mode locally)?

It seems like it means that the http and icon configuration parameters aren't provided, but the icon config is provided at the app module level and the HttpModule is imported in the IconModule where the IconService is provided.

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

And the 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";
AT82
  • 71,416
  • 24
  • 140
  • 167
tallkid24
  • 1,697
  • 4
  • 16
  • 20

6 Answers6

6

Fixed this by providing the 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 guess for some reason the Http wasn't being provided (even though HttpModule was imported) so I had to declare it as a dependency.

tallkid24
  • 1,697
  • 4
  • 16
  • 20
5

One possible cause of this error is if you are not decorating your IconService class with @Injectable(). If that is the cause, adding that decoration above the class declaration will fix the error.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
2

I have encountered similar problem. I solved it by changing export order in the barrel.

Basic service files:

// dependency.service.ts
@Injectable()
export class DependencyService { }

// dependant.service.ts
import { DependencyService } from '.';

@Injectable()
export class DependantService {
    constructor(private dependency: DependencyService) { }
}

Following barrel causes the error:

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

While following one works:

// index.ts
export * from './dependency.service';
export * from './dependant.service';
LoganMzz
  • 1,597
  • 3
  • 18
  • 31
  • I just have experienced this issue, and I wonder if you know why it happens ? – Jacques Cornat Sep 07 '17 at 20:23
  • @JacquesCornat I haven't investigated on it. At least I find logical that dependency order must be correct. However, I suspect how decorators are process. – LoganMzz Sep 08 '17 at 08:49
1

Sometimes is single way to fix it - manually describe parameters.

static get parameters() { return [Http, IconConfiguror] }

constructor(private http:Http, private iconConfiguror:IconConfiguror) {
Alex Maximenko
  • 121
  • 1
  • 5
0

Worked for me when I used following while importing the Service in app.module.ts

{provide: AuthService,
    depends: HttpClientModule}
Rakesh
  • 4,004
  • 2
  • 19
  • 31
0

My issue was that I was inheriting from a base class, and I had decorated that base class with @Injectable. The inheriting class was the class that should have the @Injectable attribute, not the base class. It seems that when the compiler sees the @Injectable attibute, it checks to make sure all the properties in the constructor can be injected. If not, it's an error. I solved it by removing the @Injectable attibute from that class.

tone
  • 1,374
  • 20
  • 47