2017-08-28 13 views
0

異なるモジュール間で再利用したいローディング画面コンポーネントがあります。Angular2モジュール内のコンポーネントのインポート

私はReportsComponent HTMLで

 @NgModule ({ 
    declarations: [ 
    ReportsComponent 
    ], 
    }); 
export class ReportsModule { 
} 

を持ってReportsModuleでAppModule

@NgModule ({ 
    declarations: [ 
    LoadingScreenComponent //has tag app-loading-screen 
    ], 
    imports: [ 
    ReportsModule,DashboardModule 
    ] 

    }); 
export class AppModule { 
} 

を持って、このようにそれをやったとき

<app-loading-screen></app-loading-screen> 

ファイルエラーにその

を取得しています
'app-loading-screen' is not a known element 

異なるモジュールのいくつかの他のコンポーネントも、ローディング画面コンポーネントを使用する必要があります。

これはまだ失敗しましたが、私はLoadingScreenComponentをルートモジュールに含めました。または私はそれについてどうやって行くのですか?

+0

@NgModule({ imports: [ //If needed ], declarations: [ LoadingScreenComponent ], exports:[LoadingScreenComponent] }) export class SharedModule {} 
AppModule

@NgModule ({ imports: [ SharedModule ] }); export class AppModule {} 

shared.module.tsはあなたにもLoadingScreenComponentためのコードを提供していただけますか? –

+0

コンポーネントには読み込みを示すテキストだけがあります。 –

+0

共有モジュールを作成して宣言し、LodingScreenModuleをエクスポートして、AppModuleとReportsModuleの両方を追加します。 – Sreemat

答えて

1

あなたが共有モジュールとあなたとしてLoding画面部品を行うことができます輸入共有モジュールの両方のアプリモジュールおよびレポートモジュール

import { NgModule} from '@angular/core'; 
@NgModule({ 
imports: [ 
], 
declarations: [ 
LoadingScreenComponent 
], 
providers: [ 

], 
exports: [ 
    LoadingScreenComponent 
] 
}) 
export class SharedModule { } 

は、その後、あなたが両方ダッシュボードモジュールおよびレポートモジュール

0

NgModulebootstrap: [ LoadingScreenComponent ]を追加します。

編集が定義されているもの、あなたのインデックスにも

@NgModule ({ 
    declarations: [ 
    LoadingScreenComponent //has tag app-loading-screen 
    ], 
    imports: [ 
    ReportsComponent,DashboardModule 
    ], 
    bootstrap: [ LoadingScreenComponent ] 

    }); 
export class AppModule { 
} 

通常、レポートhtmlをメインコンポーネントに追加するのは、それ以外の方法ではありません。

2

LoadingScreenComponentはAppModuleで宣言されていますが、AppModuleにインポートされたReportsModuleはLoadingScreenComponentについて認識していません。両方に共通のモジュールを作成し、そこにLoadingScreenComponentをインポートするには、リファクタリングする必要があります。

+0

これが解決してくれてありがとう、サンプルコードが提供されました下の@robertによって。 –

0

以下のように、LoadingScreenComponentにセレクタを含めてください。 セレクタはAppModuleにLoadingScreenComponent輸出にアレイを追加成分

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

@Component({ 
    selector: 'app-loading-screen', 
    templateUrl: 'loading-screen.component.html' 
}) 

export class LoadingScreenComponent {} 
+0

これは、角度cliを持つコンポーネントを生成した後に既に含まれています –

1

にアプリに使用されます。これは、それが世界的にアクセスできるようになります。

import { NgModule } from '@angular/core'; 
import { LoadingScreenComponent } from '...'; //Path to the LoadingScreenComponent 

@NgModule({ 
    declarations: [ 
     LoadingScreenComponent 
    ],  
    exports: [ 
     LoadingScreenComponent 
    ] 
}) 
export class SharedModule { } 

およびインポート:

@NgModule({ 
    declarations: [ 
     LoadingScreenComponent //has tag app-loading-screen 
    ], 
    imports: [ 
     ReportsModule, 
     DashboardModule 
    ], 
    exports: [ 
     LoadingScreenComponent 
    ] 
}) 
export class AppModule { 
} 

をそれ以外の場合は、最良の方法は、共有モジュールを作成し、LoadingScreenComponentを使用する他のモジュールにそのモジュールをインポートすることですそれこのような他のモジュールへ:

import { SharedModule } from '...'; //Path to the SharedModule 

@NgModule({ 
    declarations: [ 
     ReportsComponent 
    ], 
    imports[ 
     SharedModule 
    ] 
}) 
export class ReportsModule { } 
1

に共有モジュールをインポートすることができますが、他のモジュール内の共有このようなモジュールとインポートを作成します。 ReportModule

@NgModule ({ 
    declarations: [ 
    SharedMoudule, 
    ReportsComponent 
    ], 
    }); 
export class ReportsModule {} 
関連する問題