0

With a model hierarchy like this:

export class A {
   constructor() {
      // Here service X is needed for some own functions
   }
}

export class B extends A {
   constructor() {
      // Here service Y is needed for some own functions
      super( ... )
   }
}

export class C extends B {
   constructor(parent, data) {
        super( ... )
   }
}

I want to instance C objects. So the questions are:

How do i inject X and Y services?

  • Should i put all of them on the C constructor, and pass them to the base classes with super?
  • Can they be mixed with the already existing parameters parent and data ?
  • There is some way i can inject only Y at the B level, and X at the A level? So somehow i avoid passing those references on the super?
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Javier Castro
  • 321
  • 3
  • 12

1 Answers1

1
export class A {
  constructor(serviceX: ServiceX) {
    // Here service X is needed for some own functions
    this.serviceX = serviceX;
  }
}

export class B extends A {
  constructor(serviceX: ServiceX, private _serviceY: ServiceY) {
    // Here service Y is needed for some own functions
    super(serviceX);
    this.servicxY = serviceY;
  }
}

export class C extends B {
  constructor(serviceX: ServiceX, serviceY: ServiceY) {
    super(_serviceX, serviceY);
  }
}

See also Angular2 access global service without including it in every constructor

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