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
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 { ... }
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{
}
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.
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) {}
}
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.
In my case, I needed to add import "core-js/es7/reflect";
to my application to make @Injectable
work.
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