【问题标题】:Template parse: The pipe could not be found模板解析:找不到管道
【发布时间】:2017-11-08 14:16:27
【问题描述】:

我收到错误:

模板解析错误:找不到管道“amDateFormat”

这是我的app.module.ts

import { NgModule } from '@angular/core';
...
import { MomentModule } from 'angular2-moment';
...

@NgModule({
  declarations: [
    MyApp
  ],
  imports: [
    ...
    MomentModule,
    ...
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp
  ],
  providers: [
    ...
  ]
})
export class AppModule { }

然后在service-booking-details.ts 我正在做以下事情:

import { Component} from '@angular/core';
import { IonicPage, NavController, NavParams} from 'ionic-angular';
import { MomentModule } from 'angular2-moment';
...    

@IonicPage()
@Component({
  selector: 'page-item-detail',
  templateUrl: 'service-booking-detail.html'
})
export class ServiceBookingDetailPage {
  service: any;
  ...

  constructor(..., navParams: NavParams, ...) {
    this.service = navParams.get('service');
  }
}

然后在service-booking-detail.html 模板中,我尝试使用来自 angular2-moment 的管道

<ion-content>

    <ion-card>
      <ion-card-content>
         <p>{{ service.data | amDateFormat:'LL' }}</p>
      </ion-card-content>
    </ion-card>

</ion-content>

然后它抛出错误“模板解析错误:找不到管道'amDateFormat'”。

如何导入 MomentModule 以便在模板中使用它而不会出错?

【问题讨论】:

  • 请创建问题的重现以进行调试。您显示的模板内容似乎无关紧要,因为错误中提到的管道是 amDateFormat 而不是 amTimeAgo
  • 当我尝试调试和使用 angular2-moment 模块中的其他管道时,这是一个复制/粘贴错误。上面的示例在尝试使用时抛出错误 | amDateFormat:'LL' 管道也是。

标签: angular ionic2 angular2-moment


【解决方案1】:

我能够通过将其导入特定页面 *.module.ts 文件来实现此功能。

【讨论】:

    【解决方案2】:

    如果它存在于规范文件中(Jasmine,量角器)

    import { MomentModule } from "ngx-moment";
    
    beforeEach(() => {
        
          imports: [ MomentModule ]
      });
    

    为我工作

    【讨论】:

      【解决方案3】:

      在我的情况下,将 MomentModule 导入从 app.module.ts 移动到与组件相同的模块已修复。

      【讨论】:

        【解决方案4】:

        您可以将momentModule添加到特定页面模块,或者最好将其添加到共享模块

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 2019-04-25
          • 1970-01-01
          • 2017-12-08
          • 2017-05-27
          • 2018-01-03
          • 2012-02-24
          • 2019-10-09
          • 2018-09-22
          • 1970-01-01
          相关资源
          最近更新 更多