Error when trying to inject a service into an angular component “EXCEPTION: Can't resolve all parameters for component”, why?

前端 未结 30 1543
无人共我
无人共我 2020-11-22 17:18

I\'ve built a basic app in Angular, but I have encountered a strange issue where I cannot inject a service into one of my components. It injects fine into any of the three o

相关标签:
30条回答
  • 2020-11-22 17:43

    WRONG #1: Forgetting Decorator:

    //Uncaught Error: Can't resolve all parameters for MyFooService: (?).
    export class MyFooService { ... }
    

    WRONG #2: Omitting "@" Symbol:

    //Uncaught Error: Can't resolve all parameters for MyFooService: (?).
    Injectable()
    export class MyFooService { ... }
    

    WRONG #3: Omitting "()" Symbols:

    //Uncaught Error: Can't resolve all parameters for TypeDecorator: (?).
    @Injectable
    export class MyFooService { ... }
    

    WRONG #4: Lowercase "i":

    //Uncaught ReferenceError: injectable is not defined
    @injectable
    export class MyFooService { ... }
    

    WRONG #5: You forgot: import { Injectable } from '@angular/core';

    //Uncaught ReferenceError: Injectable is not defined
    @Injectable
    export class MyFooService { ... }
    

    CORRECT:

    @Injectable()
    export class MyFooService { ... }
    
    0 讨论(0)
  • 2020-11-22 17:43

    For the benefit of searchers; I got this error. It was simply a missing @ symbol.

    I.e. This produces the Can't resolve all parameters for MyHttpService error.

    Injectable()
    export class MyHttpService{
    }
    

    Adding the missing @ symbol fixes it.

    @Injectable()
    export class MyHttpService{
    }
    
    0 讨论(0)
  • 2020-11-22 17:43

    Although the ordering of exported classes from within barrels may have been mentioned, the following scenario may also produce the same effect.

    Suppose you have classes A, B, and C exported from within the same file where A depends on B and C:

    @Injectable()
    export class A {
        /** dependencies injected */
        constructor(private b: B, private c: C) {}
    }
    
    @Injectable()
    export class B {...}
    
    @Injectable()
    export class C {...}
    

    Since the dependent classes (i.e. in this case classes B and C) are not yet known to Angular, (probably at run-time during Angular's dependency injection process on class A) the error is raised.

    Solution

    The solution is to declare and export the dependent classes before the class where the DI is done.

    i.e. in the above case the class A is declared right after its dependencies are defined:

    @Injectable()
    export class B {...}
    
    @Injectable()
    export class C {...}
    
    @Injectable()
    export class A {
        /** dependencies injected */
        constructor(private b: B, private c: C) {}
    }
    
    0 讨论(0)
  • 2020-11-22 17:43

    In my case, I was exporting a Class and an Enum from the same component file:

    mComponent.component.ts:

    export class MyComponentClass{...}
    export enum MyEnum{...}
    

    Then, I was trying to use MyEnum from a child of MyComponentClass. That was causing the Can't resolve all parameters error.

    By moving MyEnum in a separate folder from MyComponentClass, that solved my issue!

    As Günter Zöchbauer mentioned, this is happening because of a service or component is circularly dependent.

    0 讨论(0)
  • 2020-11-22 17:45

    In my case, I needed to add import "core-js/es7/reflect"; to my application to make @Injectable work.

    0 讨论(0)
  • 2020-11-22 17:47

    In my case it was because of the plugin Augury, disable it will work fine. Alternative option is aot, also works.

    all credits to @Boboss74 , he posted the answer here: https://github.com/angular/angular/issues/23958

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