Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inherit two component in angular with different constructor

This is slightly similar to extending 2 class in typescript, but although I managed to do it in typescript, I can't find a correct way in angular.

I have an angular 7 app with 1 component, a service and a generic class.

Exactly like this Stackblitz

I would like to make a component that inherit from my class, AND my other component

here is my class =>

export class MyClass1<T,O> {
  propertyClass1 = "MyClass1"
  constructor() {
  }

  init(value:string){
    this.propertyClass1 = value;
  }
  sayHelloClass(value:string){console.log('class say'+ value)}
}

here my component =>

export class MyCom1Component implements OnInit {
  propertyComp1 = "MyCom1"
  constructor(private service1:Service1Service) {
      this.propertyComp1 = service1.getProp();
   }

  ngOnInit() {
  }

  sayHelloComponent(value:string){console.log('component say'+ value)}
}

I would like that my child extends booth, so that I am able to do

  ngOnInit() {
    this.sayHelloClass(this.propertyClass1); // class say MyClass1
    this.init("hoohoho");
    this.sayHelloClass(this.propertyClass1); // class say hoohoho


    this.sayHelloComponent(this.propertyComp1); // component say MyCom1
  }

what I tried is this =>

const addMyClassOneInheritance = <T extends new(...args: any[]) => any>(MyCom1Component: T) => {
  return class extends MyCom1Component {
        constructor(...args: any[]) {
            super(...args);
        }
  };
};
const MyMergedClass = addMyClassOneInheritance(MyClass1);
export class ChildComponent extends MyMergedClass<string,number> {
ngOnInit(){
   this.sayHelloClass(this.propertyClass1); // compile ok, execute give a value
   this.sayHelloComponent(this.propertyComp1); // compile ok, execute "can't find sayHelloComponent
}

}

the compiler give no error, but my component methods are not inherited

like image 461
Crocsx Avatar asked May 07 '19 03:05

Crocsx


People also ask

Is multiple inheritance possible in Angular?

Angular doesn't allow Multiple Inheritance . You can extend only one class in angular, although you can implement multiple interfaces .

Are lifecycle hooks inherited?

Lifecycle methods are not inherited Lifecycle methods (OnInit, OnChanges, …) are not inherited by the child components.

Is constructor required in Angular?

ngOnInit Angular is just a method in the class. It is directly associated with Angular and is no different from any other method in the class. It is upto the compiler if he wants to implement the method into the class or not. Constructor, on the other hand, is required to be implemented, no matter what.


1 Answers

I would suggest a different approach. Make the class available to DI by annotating it with @Injectable

Then extend your parent component as usual: ChildComponent extend MyCom1Component and inject your MyClass1 using the constructor of ChildComponent Finally call the parent's constructor with the instance of the class you just injected.

constructor(public myclass1: MyClass1) {
    super(myclass1);
  }
like image 156
guzmanoj Avatar answered Sep 18 '22 08:09

guzmanoj