In Angular 14, should an abstract base class with constructor dependencies be decorated with...

Discussão em 'Angular' iniciado por Hari Krishnan U, Abril 1, 2025.

  1. In an Angular 14 app, I have:

    @Injectable()
    export abstract class AbstractBaseService{
    constructor(
    protected override readonly serviceA: ServiceA,
    protected override readonly serviceB: serviceB
    ) {
    }
    public abstract foo();

    }


    @Injectable()
    export class ConcreteClass extends AbstractBaseService {

    constructor(
    protected override readonly serviceA: ServiceA,
    protected override readonly serviceB: serviceB
    ) {
    super(serviceA,serviceB,);
    }
    public foo(){
    }
    }


    See the base class is declared as an abstract class, which means it cannot be instantiated directly. So should it have the Injectable() decorator?. If Yes, Why?

    I removed @Injectable() from AbstractBaseService, and the app still works. Does this mean the decorator is optional? But LLMs are telling, that its ideal to have @Injectable() for the base class also. Hence the confusion.

    Continue reading...

Compartilhe esta Página