While exploring some Angular code today, I stumbled upon this interesting snippet:
export class ContentFormComponent extends FormBase {
...
constructor(
private authService: AuthService,
private apiService: ApiService,
private segmentService: SegmentService
) { super(authService, segmentService) }
...
}
The declaration of the superclass FormBaseComponent
is as follows:
export abstract class FormBase {
...
constructor (
protected authService: AuthService,
protected segmentService: SegmentService
) { }
...
}
I'm curious why this abstract class requires services from its subclasses. In Angular, services are Singleton, meaning there's only one instance throughout the app and both services are provided at root level. So, why doesn't the FormBase
class simply inject these services through DI in the constructor definition? Isn't it redundant?
This question may seem basic, but I'm a newbie and eager to learn more about Angular. Your guidance would be greatly appreciated!