2017-08-21 44 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)引導聲明。 –