How to add a custom pipe in an Angular 6 library in order for it to be available in my main app ? Currently I'm doing this in: lib.module.ts:
@NgModule({
declarations: [
SomePipe
],
exports: [
SomePipe
]})
in public_api.ts:
export * from './lib/pipes/some.pipe';
in app.module.ts:
import { LibModule } from 'lib';
@NgModule({
...
imports: [
LibModule
]
...
})
I'm trying to use the pipe {{ 'something' | some}}
but transform
method of SomePipe
is not called.
To create a custom pipe, we have to import Pipe and Pipe Transform from Angular/core. In the @Pipe directive, we have to give the name to our pipe, which will be used in our . html file. Since, we are creating the sqrt pipe, we will name it sqrt.
Pipes let you combine multiple functions into a single function. The pipe() function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.
So the issue appears to be here:
export * from './lib/pipes/some.pipe';
You are exporting everything from some.pipe
file when you should have exported the module(@NgModule
) from the module file. Something like this:
export * from './lib.module';
Doing that should fix the issue.
Here's a Sample StackBlitz for your ref.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With