2016-12-30 11 views
1

私は、最初のページに複数のコンポーネントを読み込んで表示する一般的なIonic 2アプリケーションに取り組んでいます。角2の複数コンポーネント宣言

各アドオンを簡単に作成し、アプリで簡単に実装する必要があります。そこで私は "addon-declaration.ts"という名前のファイルを作成しました。このファイルの中に、私はすべてのコンポーネントをエクスポート:

export { MyFirstAddon } from './components/addon1/first.addon'; 
export { MySecondAddon } from './components/addon2/second.addon'; 
export { MyThirdAddon } from './components/addon3/third.addon'; 

だから私の質問は、「app.module.ts」宣言フィールドに直接私のすべてのコンポーネントをインポートする方法ですか?

私はすでにこれを試みたが、働いていない:/

import * as ModuleHandler from '../models/addons/addon-declaration'; 

@NgModule({ 
    declarations: [ 
    MyApp, 
    ModuleHandler <--- Not working 
    ], 
    imports: [ 
    IonicModule.forRoot(MyApp) 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
    MyApp 
    ], 
    providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}] 
}) 
export class AppModule {} 

はそれは素敵な働いている私は一つずつをインポートする場合:

import { MyFirstAddon } from'../components/addon1/first.addon'; 
import { MySecondAddon } from'../components/addon2/second.addon'; 
import { MyThirdAddon } from'../components/addon3/third.addon'; 

@NgModule({ 
     declarations: [ 
     MyApp, 
     MyFirstAddon, <--- Working well 
     MySecondAddon, 
     MyThirdAddon 
     ], 
+0

:あなたはallAddonsオブジェクトのように使用する必要が終わりに

import * as allAddons from './addons_files'; 

:あなたはapp.module.tsのようにすべてのアドオンファイルをインポートする必要が後に続い

export * from './first.addon'; export * from './second.addon'; export * from './third.addon'; ..... 

モジュールは宣言されていませんが、 'import:[ModuleHandler]'をインポートし、 'ModuleHandler'で他のモジュールで使用できるようにコンポーネントをエクスポートします。 – Erevald

答えて

4

作成し、コンポーネントおよびディレクティブ

@NgModule({ 
    declarations: [MyFirstAddon, MySecondAddon, MyThirdAddon], 
    exports: [MyFirstAddon, MySecondAddon, MyThirdAddon] 
}) 
export class MyAddonModule {} 

はあなたが

import { MyAddonModule } from './components/my-addon-module'; 

@NgModule({ 
    declarations: [ 
    MyApp, 
    ModuleHandler <--- Not working 
    ], 
    imports: [ 
    IonicModule.forRoot(MyApp), MyAddonModule 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
    MyApp 
    ], 
    providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}] 
}) 
+0

華麗なthxたくさん! – iDev

0
declarations: [ 
    MyApp, 
    ModuleHandler.MyFirstAddon, 
    ModuleHandler.MySecondAddon, 
    ModuleHandler.MyThirdAddon 
] 

をしたりしてインポートをエクスポートngModuleを使用しますモジュール

0

あなたのアドオンを使用する他のモジュールの輸入にそのモジュールを追加エクスポート機能モジュールを作成します。あなたのすべてのアドオンファイルが置かれているそのフォルダの中にindex.tsを置き、すべてのファイルを好きなようにエクスポートするE:

@NgModule({ 
    declarations: [ 
     // all-addons 
     allAddons.FirstAddon, 
     allAddons.SecondAddon, 
     allAddons.ThirdAddon 
    ] 
}) 
export class AppModule() { 
} 
関連する問題