typescript Ionic-3 找不到管道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43299610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:26:00  来源:igfitidea点击:

Ionic-3 Can't find Pipe

angulartypescriptionic-frameworkionic3

提问by Abanoub

I have just upgraded to Ionic 3.0.1so I can use LazyLoading, and since that I can't use my custom Pipes:

我刚刚升级到Ionic 3.0.1所以我可以使用LazyLoading,因为我不能使用我的自定义Pipes

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

@Pipe({
  name: 'StripHTML'
})

export class StripHTML implements PipeTransform {

  transform(value, args) {
    let striped = value.replace(/(<([^>]+)>)/g, "");

    if (args != null) {
      if (args.split != null) {
        striped = striped.split(args.split);
        if (args.index != null) {
          striped = striped[args.index];
        }
      }
    }

    return striped;
  }
}

and in app.module.tsI have added it to the declarations :

并在app.module.ts我已将其添加到声明中:

@NgModule({
  declarations: [
    ........,
    StripHTML
  ],
...

now when am trying to use it in the htmltemplate it errors:

现在,当我尝试在html模板中使用它时,它会出错:

core.es5.js:1085 ERROR Error: Uncaught (in promise): Error: Template parse errors:
The pipe 'StripHTML' could not be found ("
          <ion-card-content>
            <ion-card-title style="font-size: 100%">
              {{ [ERROR ->]product.title | StripHTML }}
            </ion-card-title>
          </ion-card-content>
"): ng:///HomeModule/Home.html@33:17

is there anything I'm missing here?

有什么我在这里想念的吗?

回答by Abanoub

so I fixed this issue by making a PipesModulewhere I import my custom Pipesinto, then import it in the page module.tsthat I wanna use it on

所以我通过使修复了这个问题PipesModule,我输入我的自定义Pipes之中,然后将其导入的页面module.ts,我想用它

import { NgModule } from '@angular/core';
import { StripHTML } from './strip-html';

@NgModule({
  declarations: [
    StripHTML,
  ],
  imports: [

  ],
  exports: [
    StripHTML
  ]
})
export class PipesModule { }

and then in the page | HomePageas an example:

然后在页面 | HomePage举个例子:

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { Home } from './home';

import { PipesModule } from '../../pipes/pipes.module';

@NgModule({
  declarations: [
    Home,
  ],
  imports: [
    IonicPageModule.forChild(Home),
    PipesModule
  ],
  exports: [
    Home
  ]
})
export class HomeModule { }

and it did work fine , not sure if this is the correct way or not , but it worked fine, please let me know if there is a better way... thanks!

它确实工作正常,不确定这是否是正确的方法,但它工作正常,请告诉我是否有更好的方法......谢谢!

回答by Foram Sangani

What you need to do is just import the PipesModule(line 12 in below snippet) in to your every page.module.ts (i.e. home.moodule.ts) file....

您需要做的只是将 PipesModule(下面代码段中的第 12 行)导入到您的每个 page.module.ts(即 home.moodule.ts)文件中....

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { LoginPage } from './login';
import { PipesModule } from '../../pipes/pipes.module';

@NgModule({
    declarations: [
        LoginPage,
    ],
    imports: [
        IonicPageModule.forChild(LoginPage),
        PipesModule
    ]
})
export class LoginPageModule { }

This worked for me.

这对我有用。