找不到角度自定义管道 [英] Angular custom pipe not be found

查看:223
本文介绍了找不到角度自定义管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要全局使用自定义管道,我尝试按照角度管道 但我总是看到此错误

In my application I need a custom pipe globally, I try to implement it following angular pipe but i see always this error

模板解析错误:找不到管道'formatdate'

Template parse errors: The pipe 'formatdate' could not be found

formatdate.pipe

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'formatdate'
})

export class FormatdatePipe implements PipeTransform {

  transform(dateJson: any, args?: any): any {
.
 //code...
.
      return dateJson;
    }
  }
}

应用模块

import { FormatdatePipe } from './shared/pipes/formatdate.pipe';
@NgModule({
  declarations: [
    AppComponent, FormatdatePipe 
  ],

如果我在所有模块中而不是在主app.module中导入此管道,则该管道有效,我是否需要常规管道模块或其他东西

This pipe works if I import it in all my module and not in the principal app.module, do I need a routin pipe module or something

推荐答案

管道(如组件和指令)不能像服务那样​​在全球范围内工作.

Pipes (like Components and Directives) don't work globally like services do.

您需要在某些模块中定义管道.然后,您可以在该模块中定义的组件中使用它.另一种方法是将管道添加到模块的导出中,然后将该模块导入要使用它的模块中.

You need to define a pipe in some module. Then you can use it in components defined in that module. Another way is to add the pipe to exports of a module and then import that module in the module where you want to use it.

这样定义它:

import { FormatdatePipe } from './shared/pipes/formatdate.pipe';

@NgModule({
  declarations: [
    FormatdatePipe 
  ],
  exports: [
    FormatdatePipe
  ]
})   
export class SomeUtilModule {}

然后将该模块导入您要使用的模块,它应该可以工作:)

Then import this module where you want to use it and it should work :)

这篇关于找不到角度自定义管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆