网站搜索

angular - angular2 中管道的 @component 不起作用


我不清楚管道中的组件:

[] 错误

[错误信息] 类型为 '{ selector: string; templateUrl: string; pipes: any[]; }' 的参数不能分配给类型 'Component' 的参数。对象字面量只能指定已知属性,而 'pipes' 在 'Component' 类型中不存在。

notice-list.component.ts

import {Component, OnInit} from '@angular/core';
import {ocNotice} from './product';
import {NoticeFilterPipe} from './product-filter.pipe';
import {NoticeService} from './product.service'

@Component({
    selector : 'pm-products',
    templateUrl : './app/products/product-list.component.html',
    pipes: [ noticeFilter ]
})

export class ProductListComponent{
    pageTitle : string = 'Notice List';
    imgW : number = 30;
    imgH : number = 30;
    showImage : boolean = false;
    listFilter : string;
    notice : ocNotice[];

constructor(private _noticeService : NoticeService){

}
toggleImage() : void{
    this.showImage = !this.showImage;
}

ngOnInit() : void{
    console.log("Oninit");
    this.notice = this._noticeService.getProduct();
}

product-filter.pipe.ts

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

@Pipe({
    name : 'noticeFilter'
})

export class NoticeFilterPipe implements PipeTransform{
    transform(value : ocNotice[], args : string) : ocNotice[]{
        let filter : string = args[0] ? args[0].toLocaleLowerCase() : null;
       return filter ? value.filter((notice : ocNotice) => 
           notice.title.toLocaleLowerCase().indexOf(filter) != -1) : value;
    }
}

答案#1

如果你收到此错误:

类型为 '{ selector: string; templateUrl: string; pipes: typeof MattDamon[]; }' 的参数不能分配给类型 'Component' 的参数。对象字面量只能指定已知属性,而 'pipes' 在类型 'Component' 中不存在。

不要在 @component 的 pipes[] 数组中添加管道,而是将管道添加到 'app.modules.ts' 中的 'declarations' 数组中,并且不需要使用 'pipes' 数组