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.

Base-Components with @Injectable()

See original GitHub issue

To be honest I’m not pretty sure if this is a bug or a support request, but looking for an answer via stackoverflow https://stackoverflow.com/questions/60116361/angular-9-basecomponent-with-injectable?noredirect=1#comment106328351_60116361 didn’t gave me a reliable answer.

In the latest version of Angular8 base components (classes my components inherit from) needed to be Injectable. So I put an “@Injectable()” to all my base components. With Angular 9 the compiler now says:

The component YourComponent inherits its constructor from BaseComponent, but the latter does not have an Angular decorator of its own. Dependency injection will not be able to resolve the parameters of BaseComponent’s constructor. Either add a @Directive decorator to BaseComponent, or add an explicit constructor to RoleSelectDialogComponent.

The problem occurs if a component without a specific constructor inherits from a BaseComponent with constructor. Right now the problem can be fixed by replacing “@Injectible” either by “@Directive()” or “@Component({selector:‘dummy’,template:‘no-ui’})” but both seam pretty hacky - it is neither a directive nor a component. Shouldn’t “@Injectable” still suite this issue?

🔬 Minimal Reproduction

export abstract class BaseComponent implements OnInit, OnDestroy, AfterViewInit {
    constructor(
        private _baseComponentDependencyProvider: BaseComponentDependencyProvider
    ) { ...}
}

@Component({ ... })
export class MyComponent extends BaseComponent {}

🔥 Exception or Error


The component YourComponent inherits its constructor from BaseComponent, but the latter does not have an Angular decorator of its own. Dependency injection will not be able to resolve the parameters of BaseComponent's constructor. Either add a @Directive decorator to BaseComponent, or add an explicit constructor to RoleSelectDialogComponent.

🌍 Your Environment

Angular Version:


Angular CLI: 9.0.1
Node: 10.15.0
OS: win32 x64

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Feb 12, 2020

But that sounds a bit hacky for me, since it is neither a Directive nor a Component (just a base-class that could get used by both)? And there is no best pratice for components on the linked website https://angular.io/guide/ivy-compatibility-examples#undecorated-classes.

The latest version of Angular 8 had the same behavior to force people to code icy-compatible code. But there was an @Injectable() possible. Anyway - just go with @Directive() and don’t ask?

Edit: regarding the “Background” of this article an “@Injectable()” on the base class should still do the trick - but doesn’t?

1reaction
JoostKcommented, Feb 12, 2020

Small correction to Pawel’s answer: you’d need to add an @Directive() decorator, not @Component() as a template is required for components.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency injection in action - Angular
If the base component has injected dependencies, you must re-provide and re-inject them in the derived class and then pass them down to...
Read more >
Angular 9 BaseComponent with @Injectable() - Stack Overflow
I'm just going through an upgrade now, and my base component automatically has the @Directive() decorator added.
Read more >
Angular Inject & Injection Functions - Patterns & Anti-Patterns
An Angular Injection Function is a synchronous function that directly or indirectly injects services using the inject() function. Angular ...
Read more >
Injecting dependencies into Blazor components
Or we can register an interface as injectable, as long as we additionally specify the class that implements the interface. service.AddSingleton<IToDoApi, ...
Read more >
Angular Dependency Injection: Complete Guide
Components Hierarchical Dependency Injection - an example ... else in your code base, thanks to the use of the @Injectable() decorator.
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