S
S
Sergei Abramov2020-05-10 00:56:24
Angular
Sergei Abramov, 2020-05-10 00:56:24

How to inherit a constructor?

I don't really understand how to properly use IoC in service inheritance. There is a basic service:

export abstract class BaseService {
    protected constructor(protected httpClient: HttpClient) {}
}


I want to create an heir:

export class AuthService extends BaseService {
    getMember() {
         return this.httpClient.get('/member');
    }
}


But I can't, because httpClient property not found. In the heir, you need to add a constructor:

protected constructor(protected httpClient: HttpClient) { super(httpClient); }


And so it is with every successor. And here comes the moment that a new dependency needs to be added to BaseService. And because of this, you will have to copy-paste this dependency in all heirs. If we take the similar functionality of PHP, then there the constructor is forwarded to the heirs, and there is no need to deal with such copy-paste. Maybe I'm doing something wrong, and I need to inherit the constructor differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-05-10
@PatriotSY

export abstract class BaseService {
  protected httpClient: HttpClien
}

export class AuthService extends BaseService {
    constructor(
      protected httpClient: HttpClien
    ) { super() }
}

in general this is a bad approach. BaseService is not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question