Custom pipe not correctly imported [duplicate]

2019-07-06 22:05发布

问题:

This question already has an answer here:

  • How to iterate object keys using *ngFor? 4 answers

I have to iterate over an object keys inside a template, so I made this custom pipe :

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

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

And inside my page it's imported as follow :

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

But when i build project there is this error that occurs :

Argument of type '{ selector: string; templateUrl: string; pipes: typeof KeysPipe[]; }' is not assignable to parameter of type 'Component'. Object literal may only specify known properties, and 'pipes' does not exist in type 'Component'.

Any idea ? I didn't declare it in app.module.ts nor app.component.ts. Thanks.

回答1:

There is no pipes in @Component() anymore since quite some time.

Now it's

@NgModule({
  declarations: [ KeysPipe ]

See also Select based on enum in Angular2