【问题标题】:Why can't my Angular2 rc5 component find my shared pipe?为什么我的 Angular2 rc5 组件找不到我的共享管道?
【发布时间】:2016-12-26 21:30:39
【问题描述】:

我正在将一个项目更新到 rc5,并首先创建一个主模块,该模块正在为我的整个应用程序加载所有指令、管道等。我启动并运行它,一切都很好。我现在正尝试将该主模块拆分为对我的应用程序的结构和使用有意义的较小模块。我希望我的应用程序有一个模块,它只存储由多个组件共享的所有管道,所以我制作了这个模块:

pipes.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, provideForms } from '@angular/forms';

import { AbbrDay } from './abbr-day.pipe'
import { TitleCase } from './title-case.pipe'
import { ToDate } from './to-date.pipe'

@NgModule({
    declarations: [
        AbbrDay,
        TitleCase,
        ToDate
    ],
    imports: [
        BrowserModule,
        FormsModule
    ],
    providers: [
        provideForms
    ]
})

export class PipesModule {

}

在另一个模块中我想使用 TitleCase 管道,所以我尝试导入 PipesModule:

日历.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, provideForms } from '@angular/forms';
import { MODAL_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap';
import { Schedule } from 'primeng/primeng';

import { 
    AppointmentConfirmComponent, 
    AppointmentDetailComponent,
    CalendarBodyComponent,
    CalendarComponent,
    CalendarService
} from './'

import { PipesModule } from '../../shared/pipes/pipes.module'

import { SearchIdentitiesComponent } from '../identity'

@NgModule({
    declarations: [
        AppointmentConfirmComponent, 
        AppointmentDetailComponent,
        CalendarBodyComponent,
        CalendarComponent,
        MODAL_DIRECTIVES,
        Schedule,
        SearchIdentitiesComponent
    ],
    imports: [
        BrowserModule,
        FormsModule,
        PipesModule
    ],
    providers: [
        provideForms
    ]
})

export class CalendarModule {

}

当我运行应用程序时,我在运行时收到此错误:

zone.js:478 Unhandled Promise rejection: Template parse errors:
The pipe 'titleCase' could not be found ("ties" 
                  class="tag black-text pointer" [ngClass]="{'strike': participantsDiff()}">[ERROR ->]
                {{identityHelperService.getName(identity) | titleCase}}
              </span>

我是否缺少我的管道模块需要将其管道提供给其他模块的其他提供程序?

【问题讨论】:

    标签: angular typescript module pipe


    【解决方案1】:

    看起来您只需将exports 添加到您的 PipesModule 中

    exports: [
        AbbrDay,
        TitleCase,
        ToDate,
    ]
    

    您还应该使用CommonModule 而不是BrowserModule

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 2018-01-14
      • 2012-10-05
      • 2023-03-14
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      相关资源
      最近更新 更多