Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override child ngOnInit method

I want to add code to child's ngOnInit method from a parent class. Can I somehow do something similar to this?

export class Child extends Parent {
   constructor(){
      super(this.ngOnInit);
   }
   ngOnInit(){
      console.log('Child onInit');
   }
}

And Parent:

export class Parent {
   constructor(childOnInitMethod: any){}
   ngOnInit(){
      console.log('Parent added code to child onInit');
      this.childOnInitMethod.apply(this, arguments)
   }
}

The problem is that on the child constructor I do not have "this" available.

UPDATE: Find here the current code

For now, this is what I've done. Child class extends parent's ngOnInit and ngOnDestroy. What I want to achieve is to avoid calling parent "super.subscribeEvents();" like this:

ngOnInit() {
   window.onresize = () => console.log('resizing');
   super.subscribeEvents();
}

What I want to do is to add code to the Child's ngOnInit from Parent. These are the classes:

export class DatatablePage extends EventSubscriber {
   @Output() onLoadRow: EventEmitter<any> = new EventEmitter();
   public gridApi: GridApi;
   constructor(
    events: Events
   ) {
    super(events, [
        { eventName: 'datatable:updateList', callbackFunction: () => this.onLoadRow.emit() },
        { eventName: 'datatable:resizeTable', callbackFunction: () => this.gridApi.sizeColumnsToFit() }
    ])
   }

   ngOnInit() {
       window.onresize = () => console.log('resizing');
       super.subscribeEvents();
   }
}

And Parent:

export class EventSubscriber {


constructor(
    private events: Events,
    public eventSubscriptions: EventObject[]
) {
}

ngOnInit() {
    this.subscribeEvents();
}

ngOnDestroy() {
    this.unsubscribeEvents();
}

subscribeEvents() {
    this.eventSubscriptions.forEach((eventSubscription: EventObject) => {
        this.events.subscribe((eventSubscription.eventName), eventSubscription.callbackFunction)
    });
}

unsubscribeEvents() {
    this.eventSubscriptions.forEach((eventSubscription: EventObject) => {
        this.events.unsubscribe((eventSubscription.eventName), eventSubscription.callbackFunction)
    });
}
}
like image 288
Danel Ceresa Avatar asked May 16 '26 23:05

Danel Ceresa


1 Answers

the below solution should work.

class Parent {
   constructor(){}
   ngOnInit(){
      console.log('Parent added code to child onInit');
      this.ngOnInit()
   }
}

class Child extends Parent {
    ngOnInit() {
      console.log('Child onInit');
    }
    invokeSuperOnInit() { 
        super.ngOnInit();
    }
}

const a = new Child();
a.invokeSuperOnInit();

Execution:

  1. Created object of Child Class and assigned to a.

  2. calling invokeSuperOnInit Method on the object a.

  3. ngOnInit of the Parent is invoked.

  4. In the parent NgOninit We are calling ngOnInit of the Child. The Child ngOnInit is invoked because "this" will always stay same through out the hierarchy.

like image 70
Punith Mithra Avatar answered May 18 '26 12:05

Punith Mithra