1

I'm using the following mixing from this post which gives my components access to a set of reusable methods, inlcuding calling upon the lifecycle hooks.

However with AOT build, it seems that OnInit and OnDestroy wont' get triggered. Is there a solution to enforce it?

export function Destroyable<T extends Constructor<{}>>(Base: T) {
  return class Mixin extends Base implements OnDestroy {
    private readonly subscriptions: Subscription[] = [];

    protected registerSubscription(sub: Subscription) {
      this.subscriptions.push(sub);
    }

    public ngOnDestroy() {
      this.subscriptions.forEach(x => x.unsubscribe());
      this.subscriptions.length = 0; // release memory
    }
  };
}

in any component

export class CMP extends destroyable(){
     constructor(){ super() }
}
Han Che
  • 8,239
  • 19
  • 70
  • 116

0 Answers0