3

Let's say I have a Base Component -

export class BaseComponent {

    public constructor(public myService: MyService) {

    }

}

And a Derived Component -

export class DerivedComponent extends BaseComponent {

    public constructor(public myService: MyService) {
        super(myService);
    }

}

But I only really need the myService dependency in BaseComponent. Is there any way to avoid having to add the extra constructor to DerivedComponent?

Removing the dependency from the DerivedComponent seems to result in it not being injected.

gamesmad
  • 399
  • 1
  • 2
  • 14

2 Answers2

3

There are already similar questions with extensive discussion.

The answer is basically - no, you can't.

Haven't found the others.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

you can't remove the constructor from the DerivedComponent class. But with your code, you have two property named 'myService' in DerivedComponent class. You can simplify the constructor :

export class DerivedComponent {

    public constructor(myService: MyService) {
        super(myService);
    }

}
t.ouvre
  • 2,856
  • 1
  • 11
  • 17