【发布时间】:2020-09-22 00:31:48
【问题描述】:
我正在尝试在我的 Ionic 应用程序中使用管道,经过长时间的反复试验,我找到了这个教程:
https://levelup.gitconnected.com/create-your-own-pipe-in-angular-8-35f1f969ec49
它看起来很有希望,但它不起作用。我遵循了货币管道,但当然如果可行,因为货币是内置管道。一旦我将管道重命名为 customcurrency,它就会给出与所有其他教程相同的错误:
core.js:6260 ERROR 错误:未捕获(承诺):错误:管道 找不到“自定义货币”!错误:管道“customcurrency” 找不到! 在 getPipeDef$1 (core.js:36866) 在 ɵɵpipe (core.js:36824) 在 PostPage_ng_container_9_Template (template.html:33) 在执行模板 (core.js:12129) 在渲染视图(core.js:11899) 在 TemplateRef.createEmbeddedView (core.js:15576) 在 ViewContainerRef.createEmbeddedView (core.js:15687) 在 NgIf._updateView (common.js:4785) 在 NgIf.set ngIf [as ngIf] (common.js:4750) 在 setInputsForProperty (core.js:13859) 在 resolvePromise (zone-evergreen.js:798) 在 resolvePromise (zone-evergreen.js:750) 在 zone-evergreen.js:860 在 ZoneDelegate.invokeTask (zone-evergreen.js:399) 在 Object.onInvokeTask (core.js:41640) 在 ZoneDelegate.invokeTask (zone-evergreen.js:398) 在 Zone.runTask (zone-evergreen.js:167) 在 drainMicroTaskQueue (zone-evergreen.js:569) 在 ZoneTask.invokeTask [作为调用] (zone-evergreen.js:484) 在 invokeTask (zone-evergreen.js:1621)
管道:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'customcurrency'
})
export class CustomCurrencyPipe implements PipeTransform {
transform(value: number): any {
return "$" + value;
}
}
app.module:
@NgModule({
declarations: [AppComponent, CustomCurrencyPipe],
entryComponents: [],
imports:
[
CommonModule,
ReactiveFormsModule,
BrowserModule,
HttpClientModule,
IonicModule.forRoot(), AppRoutingModule
],
providers: [
CustomCurrencyPipe,
StatusBar,
SplashScreen,
HttpClientService,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {
}
用法:
{{ balance | customcurrency }}
更新
我试图在 StackBlitz 上重现我的错误:
https://stackblitz.com/edit/ionic-tup4fz
你猜怎么着?有用!所以,真正的问题是;为什么它在我的 Ionic 应用程序中不起作用?
更新 2
我开始了一个新的 Ionic 项目,添加了一个简单的管道……仅此而已。同样的问题:错误错误:未捕获(承诺):错误:找不到管道'customCurrency'!
我做了什么:
- 启动新的 Ionic 应用程序:离子启动 PipeTest 空白
- 添加了管道:离子生成管道管道/自定义货币
- 调用 home.page.html 中的应用程序:{{ balance |自定义货币 }}
- 在 home.page.ts 中添加余额:balance: number = 500;
- 完成
【问题讨论】:
-
我之前的回答可能对你有用stackoverflow.com/a/50020420/9590251
标签: angular ionic-framework pipe