The pipe ' ' could not be found angular2 custom pipe

后端 未结 9 1129
时光说笑
时光说笑 2020-11-27 14:33

I can\'t seem to fix this error. I have a search bar and an ngFor. I am trying to filter the array using a custom pipe like this:



        
9条回答
  •  孤独总比滥情好
    2020-11-27 14:48

    For Ionic you can face multiple issues as @Karl mentioned. The solution which works flawlessly for ionic lazy loaded pages is:

    1. Create pipes directory with following files: pipes.ts and pipes.module.ts

    // pipes.ts content (it can have multiple pipes inside, just remember to

    use @Pipe function before each class)
    import { PipeTransform, Pipe } from "@angular/core";
    @Pipe({ name: "toArray" })
    export class toArrayPipe implements PipeTransform {
      transform(value, args: string[]): any {
        if (!value) return value;
        let keys = [];
        for (let key in value) {
          keys.push({ key: key, value: value[key] });
        }
        return keys;
      }
    }
    

    // pipes.module.ts content

    import { NgModule } from "@angular/core";
    import { IonicModule } from "ionic-angular";
    import { toArrayPipe } from "./pipes";
    
    @NgModule({
      declarations: [toArrayPipe],
      imports: [IonicModule],
      exports: [toArrayPipe]
    })
    export class PipesModule {}
    
    1. Include PipesModule into app.module and @NgModule imports section

      import { PipesModule } from "../pipes/pipes.module"; @NgModule({ imports: [ PipesModule ] });

    2. Include PipesModule in each of your .module.ts where you want to use custom pipes. Don't forget to add it into imports section. // Example. file: pages/my-custom-page/my-custom-page.module.ts

      import { PipesModule } from "../../pipes/pipes.module"; @NgModule({ imports: [ PipesModule ] })

    3. Thats it. Now you can use your custom pipe in your template. Ex.

    {{ prop.key }}

提交回复
热议问题