javascript - 找不到 Angular2.0.0 管道 ''

标签 javascript angular typescript

错误:

Error: Template parse errors: The pipe 'datefromiso' could not be found

管道:

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

    @Pipe({
        name: 'datefromiso'
    })
    export class DateFromISO implements PipeTransform {
        transform(value: any, args: string[]): string {
            if (value) {
                var date = value instanceof Date ? value : new Date(value);
                return date.getDate() + '/' + (date.getMonth()+1) + '/' + (date.getYear()+1900);
            }
        }
    }

应用模块:

import { DateFromISO } from './pipes/date-from-iso';
...
@NgModule({
  bootstrap: [ App ],
  declarations: [
    App,
    ErrorComponent,
    DateFromISO
  ]

HTML:

<div class="pull-right">{{entity.ts | datefromiso}}</div>

entity.ts 是 ISO 字符串。怎么了? 还有一个问题:是否有更好的方法将 ISO 字符串转换为带有 angular2 的 html 中的语言环境日期? 提前致谢。

最佳答案

您需要将包含 DateFromISO 的模块添加到您使用它的每个模块的 imports 中。

因此,建议创建一个包含管道和其他可重用指令和组件的功能模块,然后将此模块添加到所有模块中应该可用的 imports

需要将管道和其他可重用组件和指令添加到此功能模块中的declarationsexports

关于javascript - 找不到 Angular2.0.0 管道 '',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40341375/

相关文章:

javascript - 错误未捕获类型错误 : Cannot read property 'checked' of null

typescript - 无法从 .ts 文件导入 .tsx 文件(反之亦然)

Angular 使用 API 调用中的 JSON 数据

javascript - 使用 javascript 打开网页时单击图像链接

angular - 如何使用 Bootstrap 4 不使用 JQuery 在 Angular 应用程序中实现自动完成输入字段

javascript - 在脚本中设置图标样式?

angular - mat-option angular 6 中大量数据的性能问题

javascript - 在 ionic 4 中使用 .js 文件

javascript - 如何在 Safari 中使用 AJAX 调用之前的 WebShareAPI?

javascript - 在初始 HTML 页面上预取 HTML 内容以供将来请求