Angular 15: Custom decorators on Properties not working

603 Views Asked by At

I follow e.g. Angular Handbook and define

export function LogClass(constructor: Function) {
    console.error('LogClass decorator executed for the constructor:');
    console.log(constructor);
}

and on my class

@Component({
   selector: ...
   templateUrl: ...
})
@LogClass    
export class MyCardComponent implements OnInit, OnDestroy {
...
    

But somehow I never see any kind of console output. This should also work according the Typescript manual. Anything I miss for using an own decorator?

UPDATE:

After trying around I think the sample is not a valid one. I guess that the statements are called just once and are not called every time the class is created as new instance - which is the main target I want to achieve!

2

There are 2 best solutions below

1
wei On

Try to put your LogClass decorator below.

You can refer to this example

https://stackblitz.com/edit/angular-decorators-example-2?file=src%2Fapp%2Fapp.component.ts

0
Marcin Milewicz On

The mentioned sample was valid in Angular 14 and previous versions. Currently, the any class decorated by Angular decorators cannot use custom decorators. Related and confirmed https://github.com/angular/angular/issues/48276