Angular2 管道找不到名称
Angular2 Pipe can't find the name
我做了一个管道如下:
import {Pipe, PipeTransform} from '@angular/core';
import { Radio } from '../../models/radio';
@Pipe({
name: 'radioFilter'
})
export class radioFilterPipe implements PipeTransform {
transform(value: Radio[], args: string[]): any {
let filter = args[0].toLocaleLowerCase();
return filter ? value.filter(radio => radio.station.text.toLocaleLowerCase().indexOf(filter) != -1) : value;
}
}
并在我的组件中添加了以下代码:
import { radioFilterPipe } from './grid.station.pipe';
// pipes: [radioFilter],
但是我得到一个编译错误:
error TS2304: Cannot find name 'radioFilter'. What am I doing wrong??
根据 Angular 2 最终版本,您必须将 Pipe
、Component
、Directive
包含到 NgModule
的 declarations
选项中元数据。
@NgModule({
declarations: [MyDirective, MyComponent, radioFilterPipe ],
providers: [radioFilterPipe], //<-- Include pipe here if you want to use it in class as dependency
imports: [ AppModule]
})
虽然在 pipes
数组中包含 pipe
名称,但它应该是 class 名称 radioFilterPipe
import { radioFilterPipe } from './grid.station.pipe';
@Component({
..,
pipes: [radioFilterPipe ],
..
})
我做了一个管道如下:
import {Pipe, PipeTransform} from '@angular/core';
import { Radio } from '../../models/radio';
@Pipe({
name: 'radioFilter'
})
export class radioFilterPipe implements PipeTransform {
transform(value: Radio[], args: string[]): any {
let filter = args[0].toLocaleLowerCase();
return filter ? value.filter(radio => radio.station.text.toLocaleLowerCase().indexOf(filter) != -1) : value;
}
}
并在我的组件中添加了以下代码:
import { radioFilterPipe } from './grid.station.pipe';
// pipes: [radioFilter],
但是我得到一个编译错误:
error TS2304: Cannot find name 'radioFilter'. What am I doing wrong??
根据 Angular 2 最终版本,您必须将 Pipe
、Component
、Directive
包含到 NgModule
的 declarations
选项中元数据。
@NgModule({
declarations: [MyDirective, MyComponent, radioFilterPipe ],
providers: [radioFilterPipe], //<-- Include pipe here if you want to use it in class as dependency
imports: [ AppModule]
})
虽然在 pipes
数组中包含 pipe
名称,但它应该是 class 名称 radioFilterPipe
import { radioFilterPipe } from './grid.station.pipe';
@Component({
..,
pipes: [radioFilterPipe ],
..
})