2017-08-21 45 views
2

有问题林在导入管与此设置的输入:我有经由与Homie.Module和Services.Module连接一dashboard.Module控制板-routing.Module意外管“ValuesPipe”由模块“HomieModule”

这是我Dashboard.Module

import { DashboardRoutingModule } from './dashboard-routing.module';  
import { ValuesPipe } from './values-pipe.pipe'; 

@NgModule({ 
    imports:  [ DashboardRoutingModule, CommonModule], 
    providers: [ HomieService, ServiceService ], 
    declarations: [ DashboardComponent, ValuesPipe], 
    exports: [ValuesPipe], 
    bootstrap: [ DashboardComponent ] 
}) 
export class DashboardModule { } 

Homie.Module

import { ValuesPipe } from './../values-pipe.pipe'; 

@NgModule({ 
    imports: [ 
    CommonModule, 
    HomieRoutingModule, 
    FormsModule, 
    Ng2SearchPipeModule, 
    ValuesPipe 
    ], 
    declarations: [HomieListComponent, HomieDetailComponent] 
}) 
export class HomieModule { } 

Service.Module

import { ValuesPipe } from './../values-pipe.pipe'; 

@NgModule({ 
    imports: [ 
    CommonModule, 
    ServiceRoutingModule, 
    FormsModule, 
    Ng2SearchPipeModule, 
    ValuesPipe 
    ], 
    declarations: [ServiceDetailComponent, ServiceListComponent] 
}) 
export class ServiceModule { } 

错误

core.es5.js:1020 ERROR Error: Uncaught (in promise): Error: Unexpected pipe 'ValuesPipe' imported by the module 'HomieModule'. Please add a @NgModule annotation. 
Error: Unexpected pipe 'ValuesPipe' imported by the module 'HomieModule'. Please add a @NgModule annotation. 

当我宣布我的烟斗在哥们和服务模块我得到的错误信息:在两个模块声明管。 所以这就是为什么我把我的管道移动到Dashboard.module这是与两个(父)连接的模块。

回答

5

按设计惯例实现的设计是错误的。 如果您想共享项目模块通用的管道,组件和指令,则应使用SharedModule概念。

在您的解决方案中,您正在正确导出管道,但正是这种方式无法正常工作。

一旦你这样做之后,出口common pipe(s), component(s) and directive(s)你必须import that entire module from where you have exported such things to other modules where you want to use them。所以千万以下,

1)在项目目录

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

import { ValuesPipe}   from './../values-pipe.pipe'; 

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

2)进口shareModule在Service.Module

import { SharedModule } from '../shared/shared.module'; 
... 
... 

@NgModule({ 
    imports: [ 
    CommonModule, 
    ... 
    SharedModule 
    ], 
    declarations: [ServiceDetailComponent, ServiceListComponent] 
}) 
export class ServiceModule { } 

创建一个共享的模块的地方现在你就可以使用出口管在Service Module

了解更多关于shareModule

+0

谢谢!我没有创建另一个模块,因为我使用DashboardModule作为共享模块。答案很完美,非常感谢! –

+0

由于共享模块包含引导进程,因此您不能拥有DashboardModule。所以不要创建一个共享模块而是创建一个新模块。 – micronyks

+0

好吧,我想我有另一个错误,然后:我有另一个模块(app.module)引导声明。 –