[英]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.
它在使用 npm i 重新安装依赖项后开始出现。
I tried removing "implements onInit" and it disappeared So the problem is related to it.我尝试删除“implements onInit”并且它消失了所以问题与它有关。 But I want to use "implements onInit" without getting this error.
但我想使用“实现 onInit”而不会出现此错误。 I suspect it has to do also with typescript version (Which i upgraded to the latest).
我怀疑它也与 typescript 版本(我升级到最新版本)有关。
Update: Of course I implemented ngOnInit function inside the class.更新:当然,我在 class 中实现了 ngOnInit function。
Error Message:错误信息:
Class is using Angular features but is not decorated.
Class 使用了 Angular 的特性,但没有装饰。 Please add an explicit Angular decorator.(-992007)?
请添加一个明确的 Angular 装饰器。(-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!从 angular 13 我猜你会收到这个错误,如果你正在使用 angular 功能,你需要一个装饰器,你可以简单地为此添加一个虚拟装饰器并解决你的问题!
@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!实现只是强制
ngOnInit
需要存在于 class 中,在 angular 组件之外没有用处!
This error happened after updating typescript to latest.将 typescript 更新到最新版本后发生此错误。 What I did is to rollback command
我所做的是回滚命令
npm i typescript@4.7.2
npm i typescript@4.7.2
And it was fixed.它是固定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.