lifecyclehooks and mixins in angular

Viewed 17

I'm trying to use mixins in angular for the first time.

I want my base class to call its mixins in an init() method:

const baseClass = mixinA(mixinB(class {
func: {A: () => void, B: () => void} = {}
init():void {
this.func.A()
this.func.B()
}

My idea was to use onInit to populate func:

export function mixinA (Base) {
return class extends Base {
a():void {
....
}
ngOnInit {
this.func = {...this.func, A: this.a}
}

mixinB following the same model.

Unfortunately, this does not work. A is properly registered in func, but B isn't. Would you have an idea why?

1 Answers

I managed to do what I wanted by using the constructor instead of onInit:

export function mixinA (Base) {
return class extends Base {
constructor(...args:any[]) {
      super()
      this.func = {...this.func, A: this.a}
}
a():void {
....
}
    }
Related