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

2

3

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};
}
Emmalineemmalyn answered 30/8, 2022 at 19:44 Comment(3)
Just revert the Typescript version to what it was maybe?Vaudeville
when u have implements onInit u must have ngOnInit function declared within class or it will errorFoss
I edited my question. I have ngOnInit() inside the class I just forgot to mention it. I want to use the latest ts version otherwise what's the point to be with lower version instead of fizong the problem?Emmalineemmalyn
E
2

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

npm i [email protected]

And it was fixed.

Emmalineemmalyn answered 31/8, 2022 at 11:42 Comment(1)
Good answer but "npm i [email protected] -D" is much more appropriate.Rapine
S
0

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!

Shalloon answered 30/8, 2022 at 20:39 Comment(2)
Adding @Directive didn't work for me. But I removed the "Implements" .. as a second thought I really don't see in this type of component a point to do so. Thank you for helpingEmmalineemmalyn
@UriGross You can also change the class to abstract class maybe that is an alternative, anyway good that its fixed!Shalloon

© 2022 - 2024 — McMap. All rights reserved.