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
Ionic-3 Can't find Pipe
提问by Abanoub
I have just upgraded to Ionic 3.0.1
so 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.ts
I have added it to the declarations :
并在app.module.ts
我已将其添加到声明中:
@NgModule({
declarations: [
........,
StripHTML
],
...
now when am trying to use it in the html
template 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 PipesModule
where I import my custom Pipes
into, then import it in the page module.ts
that 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 | HomePage
as 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.
这对我有用。