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