简体   繁体   English

Angular 10 model class 装饰器?

[英]Angular 10 model class decorator?

I recently upgraded my project from Angular 9 to Angular 10, and now adding a decorator to classes is mandatory.我最近将我的项目从 Angular 9 升级到 Angular 10,现在必须在类中添加装饰器。 I have a model class that has no decorator .我有一个没有装饰器的 model class 。 It's also a TypeScript file.它也是一个 TypeScript 文件。

Let's say I have the following model假设我有以下 model

export class Car {
   model: string;
   type: string;
   year: string;
}

Now when I run ng serve , it throws that the class has a missing decorator.现在,当我运行ng serve时,它会抛出 class 缺少装饰器。 It's not a component, nor a directive.它不是一个组件,也不是一个指令。

Specific error is the following:具体错误如下:

NG2007: Class is using Angular features but is not decorated. Please add an explicit Angular decorator.

12 export class Car {
                ~~~~~~~

It even complains about classes that I use later on to be extended in some of my components.它甚至抱怨我稍后在我的某些组件中使用的类。 Forces me to use decorator every time.强迫我每次都使用装饰器。

How can I handle this issue?我该如何处理这个问题?

According to the official docs, there's a way to handle these situations with base classes and stuff, is to add an empty Directive() decorator.根据官方文档,有一种方法可以使用基类和东西来处理这些情况,就是添加一个空的 Directive() 装饰器。

Here's the reference .这是参考

I made it work this way, and no complaints during ng serve now.我让它以这种方式工作,现在在 ng 服务期间没有任何投诉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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