自定义组件中找不到管道 - 离子3(延迟加载) [英] Pipe not found in custom component - Ionic 3 (Lazy Loading)
问题描述
在我的离子3项目中,我将我的管道包裹在pipes.module.ts中,并将其导入@NgModule中,用于我所有的延迟加载页面 - 工作正常,没有问题。
In my ionic 3 project I have my pipes wrapped in a pipes.module.ts and importing it in @NgModule for all my lazy loaded pages - works fine without issues.
但是在我的自定义组件中使用相同的方法,在组件模板中使用管道会导致错误:
However using the same approach in my custom component, using pipe inside the component template results in an error:
Error: Template parse errors: The pipe 'min2duration' could not be found
我还试图单独导入管道我的组件的.module.ts,但仍然是同样的问题。我使其工作的唯一方法是在我的组件.ts文件中导入管道,将其包装在一个函数中,我们这样的函数如下:
I tried also importing the pipe individualy in my component's .module.ts , but still same issue. The only way i made it work was to import the pipe in my components .ts file , wrap it in a function and us this function like this:
import { Min2duration } from '../../pipes/dates/min2duration';
...
@Component({ ... })
class CustomComponent {
constructor(){ ... }
min2duration(val){
var m2d = new Min2duration();
return m2d.transform(val)
}
}
并在模板中
<span>{{ min2duration(duration) }}</span>
这种方式有效,但它不是一个合适的解决方案,特别是因为我正在使用我的项目中有更多管道和更多自定义组件。
This way it works, but it doesn't feel like a proper solution, especially as i am using more pipes and more custom components in my project.
有没有办法让管道也能在这样的自定义组件模板中工作?
Is there a way to make the pipe work also inside a custom component's template like this?
<span> {{ duration | min2duration }}</span>
修改:
pipes.module.ts
pipes.module.ts
import { NgModule } from '@angular/core';
// Pipes
import { DayMonth } from './dates/day-month'
import { Weekday } from './dates/weekday';
import { Min2duration } from './dates/min2duration';
import { HighlightPipe } from './highlight/highlight';
@NgModule({
declarations: [
DayMonth,
Weekday,
HighlightPipe,
Min2duration
],
imports: [ ],
exports: [
DayMonth,Weekday,HighlightPipe,Min2duration
]
})
export class PipesModule {}
推荐答案
确保在自定义NgModule中导入pipes.module,其中包含自定义组件宣布。
Make sure you import your pipes.module inside your custom NgModule where your custom component is declared.
这篇关于自定义组件中找不到管道 - 离子3(延迟加载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!