【问题标题】:Custom pipe not correctly imported [duplicate]自定义管道未正确导入[重复]
【发布时间】:2017-04-17 08:49:19
【问题描述】:

我必须遍历模板内的对象键,所以我制作了这个自定义管道:

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

@Pipe({name: "keys"})
export class KeysPipe implements PipeTransform {
  transform(value: any, args?: any[]): any[] {
    return Object.keys(value);
  }
}

在我的页面中导入如下:

import {KeysPipe} from "../../pipes/keys-pipe";
import {Component} from '@angular/core';
@Component({
  selector: 'page-history',
  templateUrl: 'history.html',
  pipes: [ KeysPipe ]
})
export class HistoryPage {}

但是当我构建项目时会出现这个错误:

'{ selector: string; 类型的参数模板网址:字符串;管道:typeof KeysPipe[]; }' 不可分配给“组件”类型的参数。对象字面量只能指定已知属性,而“组件”类型中不存在“管道”。

有什么想法吗?我没有在app.module.tsapp.component.ts 中声明它。 谢谢。

【问题讨论】:

    标签: javascript angular typescript ionic-framework ionic2


    【解决方案1】:

    很长一段时间以来,@Component() 中没有 pipes

    现在是

    @NgModule({
      declarations: [ KeysPipe ]
    

    另见Select based on enum in Angular2

    【讨论】:

    • 哦,很好用。那我一定读过一些旧教程......非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2022-08-23
    • 2021-10-09
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多