简体   繁体   中英

How do I implement OnInit without getting error: Class is using Angular features but is not decorated. Please add an explicit Angular decorator

I would like to get rid of the Error above. It started to appear after re-installed dependencies using npm i.

I tried removing "implements onInit" and it disappeared So the problem is related to it. But I want to use "implements onInit" without getting this error. I suspect it has to do also with typescript version (Which i upgraded to the latest).

Update: Of course I implemented ngOnInit function inside the class.

Error Message:

Class is using Angular features but is not decorated. Please add an explicit Angular decorator.(-992007)?

Code:

export class SomeClass implements OnInit {
ngOnInit():void{ 
//some code};
}

From angular 13 you will be getting this error I guess, if you are using angular features you need a decorator for it, you can simply add a dummy decorator for this and solve your issue!

@Directive()
export class TestClass {

}

implements just enforces that ngOnInit needs to be present inside the class, there is no use for it outside of an angular component!

This error happened after updating typescript to latest. What I did is to rollback command

npm i typescript@4.7.2

And it was fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM