Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

508
Views
Angular 9 BaseComponent with @Injectable()

In Angular 8 I was able to create base components (classes the actual component inhert from) with an "@Injectable" attribute. The Angular 9 compiler tells me:

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.

What is the Angular 9 way of doing these things now? This works but looks somehow hacky:

@Component({
    selector: 'baseComponent',
    template: 'no-ui'
})
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The clue is in the message

Either add a @Directive decorator to BaseComponent

Adding a @Directive() to it should do the trick.

I'm just going through an upgrade now, and my base component automatically has the @Directive() decorator added.

over 4 years ago · Santiago Trujillo Report

0

This is probably going to hit more people now that Angular 10 is out and the warning is now an error.

error NG2007: Class is using Angular features but is not decorated. Please add an explicit Angular decorator.

This blog entry shows some examples https://volosoft.com/blog/what-is-new-in-angular-10


In addition please note that 'Angular features' doesn't just mean dependency injection. Even the presence of ngOnDestroy() in your base class will trigger this.

I solved it by renaming ngOnDestroy() to _ngOnDestroy() and called it from the subclass on destruction of the actual @Injectable(). This seems a little dangerous if I were to ever subclass it again and forgot to call _ngOnDestroy() but I'm not sure I had much choice.

over 4 years ago · Santiago Trujillo Report

0

This pattern has been deprecated in v9 and is no longer supported in v10.

There is a migration that typically migrates these cases automatically https://angular.io/guide/migration-undecorated-classes and https://angular.io/guide/migration-injectable, however it may be that the inherited @Injectable() case is not handled.

Anyway, the resolution here is to add @Injectable() to the abstract base class.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!