2016-11-07 4 views
0

私は角度2のアプリケーションを構築しようとしています。 私はこの基本的な構造を持っています。ネストされたモジュールからのルーティングの指定NPMパッケージを使用した角度2

AppModule

  • AppComponent
  • App.Routing
  • App.template
  • App.module

私は別のモジュールはのは、NPMパッケージとして 'SampleModule' を言わせていましたApp.moduleファイルをロードしてインポートしています

私の問題は今、 'SampleModule'内のコンポーネントを指すようにAppルーティング内のルートの1つを指定することです 'SampleComponent'。

SampleModuleからコンポーネントを公開しようとしましたが、ここではAppルーティングでインポートしようとしました。それはうまくいかなかった。それは言う、SampleComponentの名前を見つけることができません

助けを非常に感謝します。

+0

を次のコードを使用し、あなたのコードを提供することができます??。 –

答えて

0

'SampleModule'はnpmパッケージなので、 'SampleModule'のファイルはnode_modulesフォルダ(プロジェクトルートパス)の下にあります。

まず次に、あなたのApp.module、インポートで 'SampleModule'

import { SampleModule } from '/path_to_node_modules/path_to_sample_module/'; 

@NgModule({ 
    imports: [SampleModule],<== this line 
}) 
export class AppModule{} 

今あなたが直接 'SampleComponent' をインポートすることができますSampleModule

@NgModule({ 
    declarations: [SampleComponet], 
    exports: [SampleComponet] <== this line 
}) 
export class SampleModule{} 

に次の行を追加してSampleComponetをエクスポートあなたのアプリケーション。インポートステートメントに従って帰る

import { SampleComponent } from '/path_to_node_modules/path_to_componet/'; 

{ path: 'sample', component: SampleComponent } 

注: SampleModuleは独自のルーティングを持っており、それが

const routes: Routes = [~routings~] 

    @NgModule({ 
     imports: [RouterModule.forChild(routes)], 
     declarations: [SampleComponet], 
     exports: [SampleComponet] <== this line 
    }) 
    export class SampleModule{} 

以下のように定義されている場合、アプリのルータに

{ 
    path: 'sample', 
    loadChildren: './path_to_node_modules/path_to_sample_module/sample.module#SampleModule' 
} 
関連する問題