Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 library pipe

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.

like image 820
purplebuttercup Avatar asked Nov 20 '18 15:11

purplebuttercup


People also ask

How do you use pipes in Angular 6?

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.

What does pipe () Do Angular?

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.


1 Answers

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.

like image 66
SiddAjmera Avatar answered Sep 18 '22 16:09

SiddAjmera