首页 文章

不能将实体类与NgModule Angular 2一起使用

提问于
浏览
3

我正在使用Angular 2 Typescript开展项目,最近我开始使用NgModule开发app bootstraping .

我使用一些ES5类来表示服务和组件中的实体,但是外部库(例如zone.js或platform-browser)无法识别这些类 .

控制台正在记录此错误:

TypeError:无法读取null的属性'toString'

如何在Ngmodule中导出这些类,以便可以从外部源访问?

@NgModule({
   imports: [
       BrowserModule,
       RouterModule,
       FormsModule,
       HttpModule,
       JsonpModule,
       routing
   ],
   providers: [
       HeroService,
       appRoutingProviders
   ],
   declarations: [
       HeroComponent
   ],
   bootstrap:    [ AppComponent ]
})
export class AppModule { }

假设我正在使用这样的Hero类:

export class Hero {
    id: number;
    name: string;
    constructor(id: number, name: string) {
        this.id = id;
        this.name = name;
    }
}

当我尝试在服务和承诺中使用这个Hero类时,我总是得到错误,因为在NgModule定义中没有提供 .

1 回答

相关问题