如何在Angular模块中包括模型类? [英] How should I include model classes in an Angular module?

查看:183
本文介绍了如何在Angular模块中包括模型类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个类,我想只是一个普通的bean/DTO类,它们不显示@component类,它们不是@Pipe类,也不应该是@Directive(至少我不知道)认为应该!).

I've a few classes I want to be just a plain bean/DTO class, they aren't display @component classes, they're not @Pipe classes nor should they be @Directive (at least I don't think it should be!).

我希望能够将它们捆绑到一个模块中(它们将在其他模块中使用),但是尽管有很多缺点,但我仍然会遇到类似这样的错误:

I want to be able to bundle them into a module (they will be used across other modules), but despite several incantations I keep getting errors like this:

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

When I startup my Angular app (ng serve) it compiles ok, but in the browser (Chrome) console I get an error....

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

我应该如何将这些类捆绑到一个模块中以供其他模块使用?我不在乎它们是在我的服务模块中还是在其他新的"beans"模块中.

How should I be bundling these classes into a Module for use by other Modules? I don't mind if they are in my service module or in another new 'beans' modules.

给出此模块定义(service.module.ts):

Given this module definition (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:

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.它可以直接导入您的组件,指令等. 只需将其与其他导入所需的组件/服务/指令,管道一起导入即可.

You don't need to import neither declare your DTOs in app.module.ts. It's available for a direct import in your components, directives, etc.. Simply import it in whichever component/service/directive,pipes you need with other imports:

import { Accreditation } from "./accreditation";

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

If you wish to share your DTO among several modules, put it in a shared folder and access it with a relative path, i.e.

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

这篇关于如何在Angular模块中包括模型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆