2017-07-04 112 views
4

我在AppModule中有RoomsComponent,它的路線是/ rooms。 另外我有一個懶惰加載模塊CompaniesModule與組件CompaniesComponent的路線/公司。使用來自Angular 4中另一個模塊的組件

我試圖在AppModule中重新使用RoomsComponent時,建立像/ companies/{company_id}/rooms /的路線。

我不能做很長時間RoomsComponent沒有在CompaniesModule中聲明,但是這會引發錯誤,因爲組件不能在多個模塊中聲明。

+0

查看https://stackoverflow.com/questions/39601784/angular-2-use-component-from-another-module/39601837#39601837 – yurzui

回答

15

在共享模塊中聲明RoomsComponent,然後將該共享模塊導入到需要它的模塊中。以下是我的一個共享模塊的示例:

import { NgModule } from '@angular/core'; 
import { CommonModule } from '@angular/common'; 
import { FormsModule } from '@angular/forms'; 

import { StarComponent } from './star.component'; 

@NgModule({ 
    imports: [ CommonModule], 
    exports : [ 
    CommonModule, 
    FormsModule, 
    StarComponent 
    ], 
    declarations: [ StarComponent ], 
}) 
export class SharedModule { } 

請注意,我的StarComponent已在此處聲明並導出。然後,它可以用於導入此共享模塊的模塊中聲明的任何組件。

+0

好的,但如何在另一個模塊中使用該組件,如果我們不申報? – sandum

+0

如果它在一個導出它的模塊中聲明...並且我們導入該模塊......這就是我們需要的。有關模塊的更多信息,請參閱此視頻:https://www.youtube.com/watch?v=ntJ-P-Cvo7o&t=2s – DeborahK

+0

或者Angular文檔在這裏:https://angular.io/guide/ngmodule#shared -modules – DeborahK

相關問題