One of my Angular projects includes a custom pipe called CurrConvertPipe
.
import {Pipe, PipeTransform} from '@angular/core';
import {LocalStorageService} from './local-storage';
@Pipe({name: 'currConvert', pure: false})
export class CurrConvertPipe implements PipeTransform {
constructor(private currencyStorage: LocalStorageService) {}
transform(value: number): number {
let inputRate = this.currencyStorage.getCurrencyRate('EUR');
let outputRate = this.currencyStorage.getCurrencyRate(localStorage.getItem('currency'));
return value / inputRate * outputRate;
}
}
I encountered an issue while trying to use this pipe in two different modules - Module1
and Module2
. When I imported it into both modules, I received an error indicating that it should be declared at a higher level module.
To resolve this, I decided to declare the pipe within the app.module.ts
file.
import './rxjs-extensions';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CurrConvertPipe } from './services/currency/currency-pipe';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
Module1,
Module2
],
declarations: [
AppComponent,
CurrConvertPipe
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule { }
However, upon attempting to use the pipe in Module1
, another error was thrown:
The pipe 'currConvert' could not be found