2017-09-26 51 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