The pipe ' ' could not be found angular2 custom pipe

后端 未结 9 1047
时光说笑
时光说笑 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:46

    If you see this error when running tests, make sure you have imported the module the pipe belongs to, e.g.:

        beforeEach(async(() => {
            TestBed.configureTestingModule({
                imports: [CustomPipeModule],
                declarations: [...],
                providers: [...],
                ...
            }).compileComponents();
        }));
    
    0 讨论(0)
  • 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.

    <div *ngFor="let prop of myObject | toArray">{{ prop.key }}</div>

    0 讨论(0)
  • 2020-11-27 14:51

    I have created a module for pipes in the same directory where my pipes are present

    import { NgModule } from '@angular/core';
    ///import pipe...
    import { Base64ToImage, TruncateString} from './'  
    
       @NgModule({
            imports: [],
            declarations: [Base64ToImage, TruncateString],
            exports: [Base64ToImage, TruncateString]
        })
    
        export class SharedPipeModule { }   
    

    Now import that module in app.module:

    import {SharedPipeModule} from './pipe/shared.pipe.module'
     @NgModule({
         imports: [
        ...
        , PipeModule.forRoot()
        ....
      ],
    

    Now it can be used by importing the same in the nested module

    0 讨论(0)
  • 2020-11-27 14:54

    Suggesting an alternative answer here:

    Making a separate module for the Pipe is not required, but is definitely an alternative. Check the official docs footnote: https://angular.io/guide/pipes#custom-pipes

    You use your custom pipe the same way you use built-in pipes.
    You must include your pipe in the declarations array of the AppModule . If you choose to inject your pipe into a class, you must provide it in the providers array of your NgModule.

    All you have to do is add your pipe to the declarations array, and the providers array in the module where you want to use the Pipe.

    declarations: [
    ...
    CustomPipe,
    ...
    ],
    providers: [
    ...
    CustomPipe,
    ...
    ]
    
    0 讨论(0)
  • 2020-11-27 14:55

    Note : Only if you are not using angular modules

    For some reason this is not in the docs but I had to import the custom pipe in the component

    import {UsersPipe} from './users-filter.pipe'
    
    @Component({
        ...
        pipes:      [UsersPipe]
    })
    
    0 讨论(0)
  • 2020-11-27 14:57

    Make sure you are not facing a "cross module" problem

    If the component which is using the pipe, doesn't belong to the module which has declared the pipe component "globally" then the pipe is not found and you get this error message.

    In my case I've declared the pipe in a separate module and imported this pipe module in any other module having components using the pipe.

    I have declared a that the component in which you are using the pipe is

    the Pipe Module

     import { NgModule }      from '@angular/core';
     import { myDateFormat }          from '../directives/myDateFormat';
    
     @NgModule({
         imports:        [],
         declarations:   [myDateFormat],
         exports:        [myDateFormat],
     })
    
     export class PipeModule {
    
       static forRoot() {
          return {
              ngModule: PipeModule,
              providers: [],
          };
       }
     } 
    

    Usage in another module (e.g. app.module)

      // Import APPLICATION MODULES
      ...
      import { PipeModule }    from './tools/PipeModule';
    
      @NgModule({
         imports: [
        ...
        , PipeModule.forRoot()
        ....
      ],
    
    0 讨论(0)
提交回复
热议问题