Angular throws `Can't resolve all parameters` in aot when parent component has `@Injectable()` decorator
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
// base.component.ts
@Injectable()
export class BaseComponent {
constructor(
public comopnentService: ComopnentService,
) {
}
}
// child-two.component.ts
...
export class ChildTwoComponent extends BaseComponent {
}
// intermediate.component.ts
export class IntermediateComponent extends BaseComponent {
constructor(
protected type: TypesEnum,
comopnentService: ComopnentService,
) {
super(comopnentService);
}
}
// child-one.component.ts
...
export class ChildOneComponent extends IntermediateComponent {
constructor(
comopnentService: ComopnentService,
) {
super(TypesEnum.One, comopnentService);
}
}
In JIT without @Injectable()
decorator on BaseComponent
ChildTwoComponent can’t be instantiated:
Uncaught Error: Can't resolve all parameters for ChildTwoComponent: (?).
In AOT with @Injectable()
decorator warning is shown in console:
Warning: Can't resolve all parameters for IntermediateComponent in /home/sergey/projects/angular-inheritance-resolve-issue/src/components/intermediate.component.ts: (?, [object Object]). This will become an error in Angular v6.x
Expected behavior
No warnings in AOT
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
This is confusing behavior
Environment
Angular version: 5.2.4
Browser:
- [x] Chrome (desktop) version 64.0.3282.119
For Tooling issues:
- Node version: 8.9.3
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Error when trying to inject a service into an angular ...
Missing @Injectable() decorator in abstract class produced the Can't resolve all parameters for service: (?) The decorator needs be present in MyService as ......
Read more >Dependency injection in action - Angular
When a class requires a dependency, that dependency is added to the constructor as a parameter. When Angular needs to instantiate the class,...
Read more >Hierarchical injectors - Angular
The @Injectable() decorator identifies a service class. The providedIn property configures a specific ModuleInjector , here root , which makes the service ...
Read more >AOT metadata errors - Angular
Angular generates a class factory in a separate module and that factory can only access exported classes. To correct this error, export the...
Read more >InjectionToken - Angular
This sets up the InjectionToken using this factory as a provider as if it was defined explicitly in the application's root injector. If...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@WiseBird To be clear what @dewwwald explained, this is how the dependency injector works. If a class in @Injectable, Angular assumes all parameters to the constructor are determined by the injector. Since annotations are inherited, all descendants are injectable and follow the same rule.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.