I’m trying to create a custom pipe and whenever I try to invoke it in my template, I get an error:
ERROR Error: The pipe 'commaObject' could not be found!
Looking at tutorials, stack overflow posts, and here in the forums, there’s two types of solutions, and neither works for me.
Older posts show how to create the custom pipe, then load it in the app.module.ts
file then expose it through declarations
and exports
.
Newer posts say to not import your custom pipe in the app.module.ts
file, but to make a separate pipe module file (shown below) and import that only into the module/page that wants to use it.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'commaObject'
})
export class CommaObjectPipe implements PipeTransform {
transform(value: unknown, ...args: unknown[]): unknown {
return Object.keys(value).map((k) => value[k]).join(', ');
}
}
import { NgModule } from '@angular/core';
import { CommaObjectPipe } from './pipes/comma-object.pipe';
@NgModule({
declarations: [CommaObjectPipe],
imports: [],
exports: [CommaObjectPipe]
})
export class PipesModule { }
But that’s not working either. Is there a current, modern, example of how to implement Pipes with the latest Ionic/Angular?
Also, when you import the module into your page, how do you do it?
import from '../pipe.module';
???
import { PipesModule } from '../pipe.module';
???
Neither works for me, but I didn’t see the ‘right’ way to do this in the posts I looked at.