2017-09-26 33 views
0

你好,我有兩個模塊被延遲加載。你如何以角度共享模塊4

GroupModule和TaxModule。

在GroupModule中加載這個。

@NgModule({ 
    imports: [ 
    GroupRoutingModule, 
    CommonModule, 
    HttpClientModule, 
    FormsModule, 
    DataTableModule, 
    ], 
    declarations: [ 
    GroupListComponent, 
    GroupCreateComponent, 
    GroupEditComponent, 
    DataFilterPipe, 
    ], 
    providers: [GroupService] 
}) 

在我的TaxModule中我加載了這個。

@NgModule({ 
    imports: [ 
    CommonModule, 
    TaxRoutingModule, 
    HttpClientModule, 
    FormsModule, 
    DataTableModule, 
    ], 
    declarations: [ 
    TaxListComponent, 
    TaxCreateComponent, 
    TaxEditComponent, 
    DataFilterPipe, 
    ], 
    providers: [TaxService] 
}) 

問題是我得到這個控制檯錯誤。

ERROR Error: Uncaught (in promise): Error: Type DataFilterPipe is part of the 

declarations of 2 modules: TaxModule and GroupModule! Please consider moving DataFilterPipe to a higher module that imports TaxModule and GroupModule. You can also create a new NgModule that exports and includes DataFilterPipe then import that NgModule in TaxModule and GroupModule. 
Error: Type DataFilterPipe is part of the declarations of 2 modules: TaxModule and GroupModule! Please consider moving DataFilterPipe to a higher module that imports TaxModule and GroupModule. You can also create a new NgModule that exports and includes DataFilterPipe then import that NgModule in TaxModule and GroupModule. 

我將DataFilterPipe添加到根應用程序模塊,但它不工作。任何人都知道這個問題的解決方案。

的AppModule

@NgModule({ 
    imports: [ 
    BrowserModule, 
    AppRoutingModule, 
    BsDropdownModule.forRoot(), 
    TabsModule.forRoot(), 
    ChartsModule, 
    ], 
    declarations: [ 
    AppComponent, 
    FullLayoutComponent, 
    NAV_DROPDOWN_DIRECTIVES, 
    BreadcrumbsComponent, 
    SIDEBAR_TOGGLE_DIRECTIVES, 
    AsideToggleDirective, 
    ], 
    providers: [ 
    { 
     provide: LocationStrategy, 
     useClass: HashLocationStrategy, 
    }, DataFilterPipe 
    ], 
    bootstrap: [AppComponent] 
}) 

這是我app.routing

export const routes: Routes = [ 
    { 
    path: '', 
    component: LoginComponent, 
    data: { 
     title: 'Login' 
    } 
    }, 
    { 
    path: 'dashboard', 
    redirectTo: 'dashboard', 
    pathMatch: 'full', 
    }, 
    { 
    path: '', 
    component: FullLayoutComponent, 
    data: { 
     title: 'Home' 
    }, 
    children: [ 
     { 
     path: 'dashboard', 
     loadChildren: './dashboard/dashboard.module#DashboardModule' 
     }, 
     { 
     path: 'store', 
     loadChildren: './store/store.module#StoreModule' 
     }, 
     { 
     path: 'group', 
     loadChildren: './group/group.module#GroupModule' 
     }, 
     { 
     path: 'tax', 
     loadChildren: './tax/tax.module#TaxModule' 
     } 
    ] 
    } 
+0

莫非你顯示你的根AppModule的@NgModule? (只是想看看它是如何與TaxModule&GroupModule一起書寫的) – amal

回答

1

像@SergioEscudero mentoinned,可以像這樣創建用於管道的共享模塊。我已經在SharedPipesModule,這將使你的水管可見到您導入SharedPipesModule

詳情模塊增加了一個出口屬性

通知,從@PaulHalliday 結帳這個視頻https://www.youtube.com/watch?v=0NDvwt9zf9k

0

在不同的模塊不能導入的組件或管道。 您可以使用共享管道和組件創建一個模塊並將其導入到兩個模塊中,這就是全部。然後,從你

import NgModule from '@angular/core'; 

@NgModule({ 
    declarations: [  
    DataFilterPipe, 
    OtherPipeHere 
    ], 
    exports:[ 
    DataFilterPipe, 
    OtherPipeHere 
    ] 
}) 
export class SharedPipesModule{} 

的AppModule,在進口部分,添加SharedPipesModule:

+0

我是新來的Angular 4,你能給我一個例子,看看它是怎麼樣的? – user3862830