离子3:管子''无法找到

时间:2017-11-30 15:24:10

标签: angular typescript ionic-framework pipe ionic3

我似乎无法解决此问题。我已经在app.module.ts中导入并将其放入'声明'。

app.module.ts中的

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';

//ionic-native
import { NativeStorage } from '@ionic-native/native-storage';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

//pipe
import { HoursMinutesSecondsPipe } from '../pipes/hours-minutes-
seconds/hours-minutes-seconds';

@NgModule({
declarations: [
MyApp,
HoursMinutesSecondsPipe
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp, {
  platforms: {
    android: {
      tabsPlacement: 'top'
    }
  }
}),
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
NativeStorage,
]
})
export class AppModule {}

用法:

    <ion-card class="card-container" (click)="onTimer()">
      <img src="assets/imgs/wp1.png"/>
      <div class="card-title">Time Smoke Free</div>
      <div class="card-subtitle">{{ seconds | hoursMinutesSeconds }}</div>
    </ion-card>

以小时 - 分 - 秒为单位

    import { Pipe, PipeTransform } from '@angular/core';

    @Pipe({
      name: 'hoursMinutesSeconds',
    })

    export class HoursMinutesSecondsPipe implements PipeTransform {

      transform(value, args?) {

        let minutes = Math.floor(value / 60);
        let hours = Math.floor(minutes / 60);
        let seconds = Math.floor(value % 60);

        let timeString = hours + 'hrs ' + minutes + 'mins ' + seconds + 'secs';

        return timeString;

      }
    }

错误:     未捕获(承诺):错误:模板解析错误:         管道小时数秒数&#39;无法找到(&#34; imgs / wp1.png&#34; /&gt;             时间无烟             {{[ERROR - &gt;]秒| hoursMinutesSeconds}}           

1 个答案:

答案 0 :(得分:23)

使用此CLI pipe创建ionic generate pipe HoursMinutesSeconds时,它将创建一个名为pipes.module.ts的共享模块。因此,您需要在页面的模块文件中使用import PipesModule模块。

让我们认为页面是my-view然后;

MY-view.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { MyViewPage } from './my-view';
import { PipesModule } from '../../pipes/pipes.module';//<--- here

@NgModule({
  declarations: [
    MyViewPage,
  ],
  imports: [
    IonicPageModule.forChild(BudgetGroupViewPage),
    PipesModule // <--- here
  ],
})
export class MyViewPageModule { }