简体   繁体   English

如何在不出错的情况下实现 OnInit:Class 正在使用 Angular 功能但未进行装饰。 请添加显式 Angular 装饰器

[英]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.

相关问题 问:Angular 组件损坏 - “类正在使用 Angular 功能但未装饰。请添加显式 Angular 装饰器” - Q: Angular components broken - "Class is using Angular features but is not decorated. Please add an explicit Angular decorator" Angular 15: Class 正在使用 Angular 功能但未修饰。 请添加明确的 Angular 装饰器。(-992007) - Angular 15 : Class is using Angular features but is not decorated. Please add an explicit Angular decorator.(-992007) 类使用 Angular 特性但没有修饰。 请添加一个显式的 Angular 装饰器 - Class is using Angular features but is not decorated. Please add an explicit Angular decorator Angular 11 '错误 NG2007:Class 正在使用 Angular 功能但未修饰。 但是 class 是装饰的 - Angular 11 'error NG2007: Class is using Angular features but is not decorated.' But the class is Decorated Class 正在使用 Angular 功能,但未在 angular 14 中进行修饰 - Class is using Angular features but is not decorated in angular 14 Angular 库基础 class 没有任何装饰器但使用 Angular 功能 - Angular Library base class without any decorator but using Angular features Class 正在使用 Angular 功能但未使用 Typescript 4.8.2 进行装饰 - Class is using Angular features but is not decorated with Typescript 4.8.2 如何使用 typescript 中的装饰器向 class 添加和声明新属性(角度) - How to add and declare new property to class using decorator in typescript (angular) 如何不使用装饰器语法编写angular2? - How do I write angular2 without decorator syntax? Angular 8+ - 如果我不打算覆盖它,是否需要在我的组件中实现 OnInit function? - Angular 8+ - do I need to implement the OnInit function in my components if I am not planning to override it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM