Reputation: 3280
I have many child components Child1Component
, etc., how do I write the annotations for the extended parent class ParentComponent
?
/** Parent Component */
/** <----- How to annotate here? */
export class ParentComponent implements OnInit {
constructor(i: INJECTED1, otherParam: string) {
...
}
...
}
/** Child Component */
@Component({selector: ..., template: ..., styleUrls: ...})
export class Child1Component extends ParentComponent {
constructor(i: INJECTED1, j: INJECTED2) {
super(i, 'otherParam');
}
...
}
What I've tried so far (with limited success):
ParentComponent
with @Directive()
was the closest I found so far, as the angular compiler seems to process that in the desired way. However, this feels wrong, as this clearly is no angular directive. The linter is also not happy with this, expecting a *.directive.ts
ending on the filename without further tweaking.ParentComponent
at all. This results in the Angular Compiler throwing an error NG2007: Class is using Angular features but is not decorated
.ParentComponent
with @Component({template: ''})
feels much better and leaves the linter happy with the file name. But the angular compile now insists on all parameters of ParentComponent
to be injectables, which they aren't, because they get called by their super class.Upvotes: 0
Views: 439
Reputation: 4472
Yes, annotating with @Directive is the way to go (at least since angular 9 if I remember correctly, earlier versions of angular didn't need that).
You can rename the ParentComponent to ParentDirective and rename the file accordingly to make the linter happy, or you can configure the linter to ignore this case.
Edit: I prefer renaming ParentComponent to ParentDirective, because when you think about it a component is basically a directive (i.e. a selector that makes Angular treat certain html tags or html directive) but components have a template and styles while directives don't. Since you parent class doesn't have a template, it makes sense to name it directive.
Upvotes: 1