Angular 使用 ionic 4 找不到管道

Angular pipe could not be found using ionic 4

最近我在 ionic4 中创建了自己的管道,名称是 StatusairportPipe。然后我将其导入 app module.ts ,现在当我尝试使用它时出现错误:

ERROR Error: Uncaught (in promise): Error: Template parse errors:
The pipe 'my' could not be found ("
            <td text-center>{{item?.flight.aircraft.model.code}}</td>
            <td text-right>{{[ERROR ->]item?.flight.status.generic.status.text | my}}</td>

            </tr>

StatusairportPipe 模型 ts

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

@Pipe({
  name: 'my'
})
export class StatusairportPipe implements PipeTransform {

  public states: Object = {
    'scheduled':   'مجدولة',

  };
  transform(value: string, ...args) {
    // This is our catch for data that hasn't interpolated
    // from its source yet, a basic async fix.
    if(value == null) return;
// Otherwise, lookup the state name from the acronym
    if(this.states[value]){
      return this.states[value];
    } else {
      return value;
    }
  }

}

app.module.ts

   import { NgModule } from '@angular/core';
    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 { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { HTTP } from '@ionic-native/http/ngx';
    import { Network } from '@ionic-native/network/ngx';
    import { StatusairportPipe } from './statusairport.pipe';

    @NgModule({
      declarations:
       [AppComponent, 
        StatusairportPipe

      ],
      entryComponents: [],

      imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
        exports:[
    StatusairportPipe,
    AliPipe
  ],
      providers: [
        StatusBar,
        SplashScreen,
        { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
        HTTP,
        Network
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule {}

我遇到过这个问题,我只是在模块导出上添加了管道来解决问题:


@NgModule({
  declarations:
   [AppComponent, 
    StatusairportPipe

  ],
  entryComponents: [],

  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],

  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    HTTP,
    Network
  ],
  bootstrap: [AppComponent],
  exports: [ StatusairportPipe]
})
  export class AppModule {}

当我在 ionic 4 中创建自己的管道时,您只需将其添加到您要使用的页面模块中已经创建的管道中。您不需要添加 app.module

  1. 首先,在 src/app 文件夹(在 app 文件夹中)中创建管道文件夹。

  2. 其次,在命令“ionic generate pipe pipes/searchfilter”上 => 这将在管道中生成两个文件。

  3. 第三,在名为“pipes.module.ts”的管道文件夹中创建文件 并将下面的代码写入 => "pipes.module.ts"

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SearchfilterPipe } from './searchfilter.pipe';  //our pipe which we generate

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [SearchfilterPipe],
  exports: [SearchfilterPipe]
})
export class PipesModule { }

现在我们有了 PipesModule,我们可以生成更多的管道并将它们写入 pipesmodule。我们将只导入 PipesModule 而不是所有管道。

  1. 您不必在 app.module.ts

    中导入 PipesModule
  2. 现在转到您要使用管道的页面并打开例如“anasayfa.module.ts” 并导入“PipesModule”并将其写入@ngModel imports(它将自动创建) 请注意,您会将 PipesModule 导入 something.MODULE.TS 而不是 something.page.ts