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

前端之家收集整理的这篇文章主要介绍了我应该如何在Angular模块中包含模型类?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有几个类我想成为一个普通的bean / DTO类,它们不是显示@component类,它们不是@Pipe类,也不是@Directive(至少我不认为它应该是!).

我希望能够将它们捆绑到一个模块中(它们将用于其他模块),但是尽管有几个咒语,我仍然会遇到这样的错误

当我启动我的Angular应用程序(ng服务)时,它编译正常,但在浏览器(Chrome)控制台中我收到错误….

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

我应该如何将这些类捆绑到一个模块中供其他模块使用?我不介意他们是在我的服务模块中还是在另一个新的’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,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.它可用于直接导入组件,指令等.
只需将其导入任何组件/服务/指令,您需要与其他导入管道:

import { Accreditation } from "./accreditation";

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

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

猜你在找的Angularjs相关文章