【发布时间】:2020-07-23 16:17:14
【问题描述】:
我正在尝试在我的 Angular 应用程序中创建一个自定义管道,但不断收到“找不到名称为“currencyFormat”的管道”错误消息。我使用 Angular CLI 创建了管道:ng g pipe /components/pipes/currency-format,代码如下所示:
import { formatNumber } from '@angular/common';
@Pipe({
name: 'currencyFormat'
})
export class CurrencyFormatPipe implements PipeTransform {
transform(value: any, currency: string): any {
const currencySymbol = (currency == 'EUR' ? '€' : '$');
let formattedValue = `${currencySymbol} ${formatNumber(value, 'be', '1.2-2')}`;
return formattedValue;
}
}
因为我使用 Angular CLI 创建管道,所以管道会自动添加到 app.module.ts 的声明中。我正在尝试在我的一个页面(home.page.html)中使用管道,但我仍然收到此错误。到目前为止,我已经尝试了许多不同的方法,包括将管道放在单独的模块中并尝试导入该模块,但没有任何成功。
任何想法这个问题可能是什么?
编辑:这也是我的 app.module.ts,以防它有用:
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { ComponentsModule } from 'src/app/components/components.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ChartsModule } from 'ng2-charts';
import { environment } from "src/environments/environment";
import { AngularFireModule } from "@angular/fire";
import { AngularFirestoreModule } from "@angular/fire/firestore";
import { ServiceWorkerModule } from '@angular/service-worker';
import { CurrencyFormatPipe } from './components/pipes/currency-format.pipe';
@NgModule({
declarations: [AppComponent, CurrencyFormatPipe],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot({ animated: true, mode: 'ios' }),
AppRoutingModule,
ComponentsModule,
BrowserAnimationsModule,
ChartsModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFirestoreModule,
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })
],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
【问题讨论】:
-
您有
home.module.ts文件吗?如果这样做,您是否将您的CurrencyFormatPipe添加到home.module.ts声明数组中? -
嗨@DJHouse,如果我同时添加它,我会收到错误
The Pipe 'CurrencyFormatPipe' is declared by more than one NgModule. -
啊,我知道什么会起作用。我会发布一个答案。
标签: angular