I have been working on creating a custom decorator to integrate into the ngOnInit
function in Angular components. While I am successfully able to access the ngOnInit
from the prototype within my decorator, my replacement function is not being called during the component lifecycle. I'm puzzled as to why my code doesn't seem to be functioning properly. Can anyone provide insight into what might be causing this issue?
import {Component, OnInit} from '@angular/core';
function CustomDecorator( theClass: Function ) {
console.log( 'decorator called' );
const ngOnInit = theClass.prototype.ngOnInit;
theClass.prototype.ngOnInit = function() {
console.log( 'decorator nginit' );
ngOnInit();
};
}
@CustomDecorator
@Component( {
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
} )
export class AppComponent implements OnInit {
title = 'angtest';
constructor() {
console.log( 'component constructed' );
}
ngOnInit(): void {
console.log( 'component nginit' );
}
}