Angular2 - Error: Can't resolve all parameters for IconService

前端 未结 6 903
南旧
南旧 2021-01-19 00:32

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).

相关标签:
6条回答
  • 2021-01-19 00:50

    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.

    0 讨论(0)
  • 2021-01-19 00:52

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

    {provide: AuthService,
        depends: HttpClientModule}
    
    0 讨论(0)
  • 2021-01-19 00:57

    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';
    
    0 讨论(0)
  • 2021-01-19 01:06

    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.

    0 讨论(0)
  • 2021-01-19 01:08

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

    static get parameters() { return [Http, IconConfiguror] }
    
    constructor(private http:Http, private iconConfiguror:IconConfiguror) {
    
    0 讨论(0)
  • 2021-01-19 01:13

    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.

    0 讨论(0)
提交回复
热议问题