2017-02-14 42 views
2

我不知道關於在管道組件:在管道中的@Component不工作angular2

[]錯誤

[錯誤消息] 參數類型的「{選擇:字符串; templateUrl:string;管道:任何[]; }'不能分配給'Component'類型的參數。 對象字面量只能指定已知屬性,而'管道'在類型'組件'中不存在。

通知,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(); 
} 

產品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

您正在以錯誤的方式引入管道。閱讀我的答案並按照說明進行操作:http://stackoverflow.com/questions/42093552/can-i-make-global-registration-pipe-guard-for-global-access/42094880#42094880 –

+1

首先,您應該在你的'module'中聲明'pipe'。現在,如果你假裝在你的'.ts - component'文件中使用'pipe',你必須將它:'pipes:[noticeFilter]'改爲:'providers:[NoticeFilterPipe]'。如果你想在* template *中使用它,你只需要在你的'module'中聲明它。 – developer033

+2

你使用什麼版本的Angular 2?據我所知,'pipe'不再在'component'文件中聲明;它必須在'module'的'declaration'中聲明。 –

回答

0

,如果你得到這個錯誤:

類型的參數「{選擇:字符串; templateUrl:string;管道:typeof MattDamon []; }'不能分配給'Component'類型的參數。 對象字面量只能指定已知屬性,而'管道'在類型'組件'中不存在。

代替在@Component []陣列中的管道將所述管的, 添加管內「app.modules.ts」的「聲明」陣列中,並且無需使用「管道」陣列