2016-07-18 5 views
2

TypescriptでAngular2で新しいAppを開発しています。すべてのクラスとインタフェースを別々のファイルで宣言し、angular2コンポーネントでクラスとインタフェースを参照するよりクリーンな方法はありますか?別のファイルでtyperscriptクラスを作成し、それをangular2コンポーネントで使用する

import {Component, OnInit, Pipe} from 'node_modules/angular2/core'; 
import { HTTP_PROVIDERS, Http, Response, Headers } from 'node_modules/angular2/http'; 
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router } from 'node_modules/angular2/router'; 
import { Observable } from 'node_modules/rxjs/Rx'; 

@Component({ 
    selector: 'my-App', 
    templateUrl: '/app/Index.html', 
    directives: [ROUTER_DIRECTIVES, NewEvent] }) 
//instead of writing class here , is there any way to declare this class in a 
//separate file and injecting it here. 
export class Events { } 
+2

あなたのすべての輸入:

@Component({ // Applied decorator (...) }) export class Events { // Component class } 

デコレータとクラスは、他のモジュールからインポートすることができます。あなたは同じことをすることができます。 http://plnkr.co/edit/?p=previewも参照してください。 –

答えて

1

コンポーネントのクラスを適用するデコレータと同じモジュール内に宣言する必要があります。他のファイルからの行のインポートクラスの最上部に

import {Component} from '@angular/core'; // decorator from Angular2 

import {SomeOtherClass} from './some-module'; // custom class 
関連する問題