question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular throws `Can't resolve all parameters` in aot when parent component has `@Injectable()` decorator

See original GitHub issue

I’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

Reproduction

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:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
chuckjazcommented, Feb 15, 2018

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

0reactions
angular-automatic-lock-bot[bot]commented, Sep 15, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found