2016-08-22 5 views
1

나는이 줄을 옮겼으니, Ngmodule과 수입품을 연습 해 보려고합니다. (heroes.component.ts 파일에서) './hero'에서 {Hero} 가져 오기; app.module.ts 파일angular2, 영웅의 투어, ngmodule 수입

: 수입 {구성 요소,하는 OnInit :

[0] app/heroes.component.ts(20,11): error TS2304: Cannot find name 'Hero' 

그것은 구성 요소 단지 기본 가져 오기에 할 수있다 :

import { Hero } from './hero'; 
    @NgModule({ 
     imports: [ 
     BrowserModule, 
     FormsModule, 
     routing, 
     Hero 
     ], 

하지만 난이 오류가 }에서 '@ angular/core'; 그리고 나머지 모든 가져 오기를 NgModule로 이동 하시겠습니까? 아니면 내가 제한해야합니까?

답변

1

은 예, NgModule 내부에 포함해야하는지에 제한이 있습니다. Esentially 함께 모듈 그룹 기능 세트, 당신은 단지 NgModule에 구성 요소, 지침, 서비스 및 기타 모듈을 추가 할 수 있습니다, 당신의 영웅 가져 오기 구성 요소가 아닌 모듈 내부해야한다. NgModule의 때에 프로퍼티 '수입'다른 모듈을 가져올하지만 수입 부품에 대한 아니라고

//app.module.ts 
import { NgModule }  from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 

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

@NgModule({ 
    imports: [ BrowserModule ], //other modules you require 
    declarations: [ AppComponent ], //declares components and directives 
    bootstrap: [ AppComponent ] //your root component 
}) 
export class AppModule { } 
+0

감사; 의심의 여지가있다. 서비스는 NgModule의 '제공자 :'로 이동합니다. import 행은 app.module.ts로 이동하고 @ Component 데코레이터의 지시문을 NgModule의 'declarations :'로 이동하지만 NgModule의 'imports :'로 정확히 이동합니까? 결코 개인적인 구성 요소가 아닌가? – stackdave

+0

서비스를 옮길 때 구성 요소에 파일 서비스의 가져 오기 줄을 유지해야합니까? 및 NgModule 파일에서. 수입 라인과 '공급 업체 :'? – stackdave

+0

아니요, '가져 오기'에서 BrowserModule 또는 자신의 모듈과 같은 다른 모듈 만 가져옵니다. https://angular.io/docs/ts/latest/guide/ngmodule.html –

1

이유 (파일 :

import { Hero } from './hero'; 
//This import should stay inside app.component.ts 

그래서 파일은

//app.component.ts 
import { Component } from '@angular/core'; 

import { Hero } from './hero'; 

@component({ 
    selector: 'my-app', 
    template: `<p>A template goes in here</p>` 
}) 
export class AppComponent { 
    public hero: Hero; 
} 

그리고 일 것입니다 다른 구성 요소로 그룹화 된 다른 NgModule).

'imports :'속성에서 구성 요소를 Ngmodule로 옮길 수 있습니다. 가져 오기 행 : import {}에서 '..'; 및 '지시어'줄

서비스는 'providers :'속성 의 NgModule로 이동할 수 있지만 서비스가 구성 요소에서 유지해야하는 차이점은 다음과 같습니다. import {} from '..';

+0

우수! 이제 당신은 당신 자신의 질문을 해결했습니다. 계속 공부하다! –

관련 문제