我应该如何在 Angular 模块中包含模型 类?

How should I include model classes in an Angular module?

我有一些 classes 我想只是一个普通的 bean/DTO class,它们不显示 @component classes,它们'不是@Pipe classes 也不应该是@Directive(至少我认为不应该是!)。

我希望能够将它们捆绑到一个模块中(它们将在其他模块中使用),但是尽管有几个咒语,我还是不断收到这样的错误:

当我启动 Angular 应用程序 (ng serve) 时,它编译正常,但在浏览器 (Chrome) 控制台中出现错误....

Uncaught Error: Unexpected value 'Accreditation' declared by the module 'ServiceModule'. Please add a @Pipe/@Directive/@Component annotation.

我应该如何将这些 classes 捆绑到一个模块中以供其他模块使用?我不介意它们是在我的服务模块中还是在另一个新的 'beans' 模块中。

鉴于此模块定义 (service.module.ts):

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {MemberService} from "./member/member.service";
import { Accreditation } from "./accreditation";
import {Player} from "./player";
import {Club} from "./club";
import {Coach} from "./coach";

@NgModule({
  imports: [CommonModule],
  declarations: [Accreditation, Club, Player, Coach],
  exports: [Accreditation, Club, Player, Coach],
  providers: [MemberService]
})
export class ServiceModule { }

accreditation.ts:

export class Accreditation {
    uuid: string;
    name :string;
    lastComplete: Date;
    expires: Date;
    inLast6Months: boolean;
    type: String;
    displayName: String;
    hasCompleted: boolean;
}

您不需要在 app.module.ts 中导入或声明您的 DTO。它可用于直接导入您的组件、指令等。 只需将其导入您需要的任何 component/service/directive 管道和其他导入:

import { Accreditation } from "./accreditation";

如果您希望在多个模块之间共享您的 DTO,请将其放在共享文件夹中并使用相对路径访问它,即

import { Accreditation } from "./shared/accreditation";