簡體   English   中英

Angular 2 RC.5共享模塊找不到管道

[英]Angular 2 RC.5 Shared Module can't find pipes

我正在更新我的應用程序以使用模塊結構,當我嘗試將我的管道組件添加到共享模塊時,我遇到了一個奇怪的問題。 從我所讀到的,我已經把一切都設置得正確,所以我必須遺漏一些東西。

錯誤: Unhandled Promise rejection: Template parse errors: The pipe 'cmgTitleize' could not be found

我有一個BrowseModule ,這個模塊聲明一個ProjectCardComponent ,它有一個使用cmgTitleize管道的模板。 為了提供對TitleizePipe訪問,我導入了我的SharedModule

@NgModule({
  declarations: [
    ...,
    ProjectCardComponent
  ],
  imports: [
    ...,
    SharedModule
  ],
  providers: [
    ...
  ]
})

export class BrowseModule { }

SharedModule ,導入PipesModule

@NgModule({
  declarations: [
    ...
  ],
  exports: [
    ...
  ],
  imports: [
    ...,
    PipesModule
  ]
})

export class SharedModule { }

PipesModule聲明並導出TitelizePipe

@NgModule({
  declarations: [
    ...
    TitleizePipe
  ],
  exports: [
    ...
    TitleizePipe
  ]
})

export class PipesModule { }

最后,為了進行理智檢查,繼承了TitleizePipe:

@Pipe({
  name: 'cmgTitleize'
})

export class TitleizePipe implements PipeTransform {
  ...
}

看起來我只需要在PipesModule中導出SharedModule

如果您在共享模塊中使用靜態“forRoot()”,則仍需要導出管道或任何其他所需模塊:

@NgModule({
    exports: [
        MyPipesModule                   //<---------------- HERE
    ]
})
export class SharedModule { 
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: SharedModule,
            providers: [
                MySingletonServices
            ]
        };
    }
}

然后,您只需在主應用程序模塊中導入它:

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        SharedModule.forRoot()       // <------------
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM