我应该如何在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.
我应该如何被捆绑这些类到一个模块中,以供其他模块使用?我不介意他们是否在我的服务模块或其他新的“bean”模块中。
鉴于这种模块定义(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。这是一个可以在你的组件,指令等的进口对象../ 导入它在哪个组件/服务的指令,你需要与其他进口:
import { Accreditation } from "./accreditation";
基本DTO的是没有棱角的东西,所以不要”不属于Angular模块。你不能将它们添加到声明数组中。声明数组仅用于组件,指令和管道,因此您看到的错误。 – DeborahK