2016-10-26 3 views
5

사용자, 도서, 라이브러리, 영화 등과 같은 객체로 구성된 @NgModule (모델명 ModelsModule)을 만들려고했습니다. 이렇게 할 때마다 필요한 모든 개체를 가져 오지 않고 처음에 AppModule (main @NgModule)으로 가져 와서 원하는만큼 여러 번 사용하도록 노력하고 있습니다.각도 2의 NgModule에 모델/엔티티/객체 추가

객체/엔티티/클래스 ... 예

export class Author { 
    constructor (
    public name: string, 
    public avatar: string 
) { } 
} 

ModelsModule

import { NgModule } from '@angular/core'; 
import { CommonModule } from '@angular/common'; 
import { FormsModule } from '@angular/forms'; 

import { Author } from './author/author.model'; 
(...) 

@NgModule({ 
    imports: [ 
    CommonModule, 
    FormsModule, 
    Author, 
    Book, 
    Movie, 
    Store 
    ], 
}) 
export class ModelsModule {} 

AppModule

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 
import { MaterialModule } from '@angular/material'; 

import { AppComponent, SettingsDialog } from './app.component'; 

// THAT ONE 
import { ModelsModule } from '../models/models.module'; 

@NgModule({ 
    declarations: [ 
    AppComponent, 
    SettingsDialog 
    ], 
    entryComponents: [ 
    AppComponent, 
    SettingsDialog 
    ], 
    providers: [ 
    // ModelsModule (?) 
    ], 
    imports: [ 
    BrowserModule, 
    HttpModule, 
    // ModelsModule (?) 
    MaterialModule.forRoot() 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 
+0

이것은 좋은 연습이며, 실제로 각도 CLI가 어떻게 물건을 구성하는지 보여줍니다. 문제/질문은 무엇입니까? – Meir

+0

간단한 클래스를 ngModule로 가져올 수 없습니다. – MatWaligora

답변

7

모델 클래스는 각도로 표시되지 않습니다. 클래스를 필요한 파일로 가져온 다음 사용하십시오.

import { MyModel } from './my.model'; 

class SomeComponent { 
    model = new MyModel(); 
} 

초보자의 많은이 import 문이 수업을 위해 무엇에 대해 혼란스러워하는 것, 그리고 그들이 어떻게 든 어떻게 든 각도로 클래스를 가져 그들을 제거 할 수 있다고 생각합니다. 그렇지 않다. 클래스를 파일로 가져 오는 것은 Angular에만 국한되지 않습니다. 파일 가져 오기는 단순히 한 파일의 항목을 다른 파일에서 사용할 수있는 방법입니다.