简体   繁体   English

组件中的管道过滤器的angular2错误

[英]angular2 error with pipe filter in component

I'm trying to add simple search filter in input, so it could filter my records in table. 我试图在输入中添加简单的搜索过滤器,以便它可以过滤表中的记录。

But I'm receiving this kind of error: 但是我收到这种错误:

app/components/orders/orders.component.ts(12,2): error TS2345:
 Argument of type '{ moduleId: string; selector: string; templateUrl:
 string; pipes: typeof FilterPipe[]; }' is not assignable to parameter
 of type 'Component'.   Object literal may only specify known
 properties, and 'pipes' does not exist in type 'Component'.

All files: 全部文件:

orders.component.html, orders.component.ts, filter.pipe.ts are in the same folder orders.component.html,orders.component.ts,filter.pipe.ts位于同一文件夹中

And there is code in files: 文件中有代码:

HTML in orders.component.html orders.component.html中的HTML

<input class="prompt" type="text" placeholder="Szukaj zlecenia..." [(ngModel)]="term">

<tr *ngFor="let order of orders">
    <td>{{order.orderNum}}</td>
    <td>{{order.clientNum}}</td>
    <td>{{order.dateCreated}}</td>
    <td>{{order.account}}</td>
    <td>{{order.status}}</td>
</tr>

filter.pipe.ts filter.pipe.ts

 import { Injectable, Pipe, PipeTransform } from '@angular/core';

    @Pipe({
        name: 'ordersFilter',
        pure: false
    })
    @Injectable()
    export class FilterPipe implements PipeTransform {
        transform(items: any[], args: any[]): any {
            return items.filter(item => item.title.indexOf(args[0].title) !== -1);
        }
  }

orders.component.ts orders.component.ts

import { Component } from '@angular/core';
    import { OrderService } from '../../services/order.service'
    import { Order } from '../../structure/Order';
    import { Injectable, Pipe, PipeTransform } from '@angular/core';
    import { FilterPipe } from './filter.pipe';

    @Component({
        moduleId: module.id,
        selector: 'orders',
        templateUrl: 'orders.component.html',
        pipes: [FilterPipe]
    })

It looks like it doesn't like pipes: [FilterPipe] but, as far as I know it is set properly. 看起来它不像pipes: [FilterPipe]但是据我所知它设置正确。

In web browser I'm receiving this error: 在网络浏览器中,我收到此错误:

zone.js:388 Unhandled Promise rejection: Template parse errors: The
 pipe 'ordersFilter' could not be found ("      </thead>        <tbody>         <tr
 [ERROR ->]*ngFor="let order of orders | ordersFilter:term">
            <td>{{order.orderNum}}</td>             <td>{{order.clien"):
 OrdersComponent@28:6 ; Zone: <root> ; Task: Promise.then ; Value:
 Error: Template parse errors:(…) Error: Template parse errors: The
 pipe 'ordersFilter' could not be found ("      </thead>        <tbody>         <tr
 [ERROR ->]*ngFor="let order of orders | ordersFilter:term">
            <td>{{order.orderNum}}</td>             <td>{{order.clien"):
 OrdersComponent@28:6

Hint: There is no need to add @Injectable() when there is already @Pipe() , @Component() , or @Directive() 提示:无需添加@Injectable()时,已经是@Pipe() @Component()@Directive()

Ensure you have FilterPipe added to declarations: [FilterPipe] of your current module 确保已将FilterPipe添加到declarations: [FilterPipe]当前模块的declarations: [FilterPipe]

or 要么

added the module that has 添加了具有

declarations: [FilterPipe],
exports: [FilterPipe]

to imports: [...] of your current module. imports: [...]当前模块的imports: [...]

Most probably, you are using ngModule approach for your app if so than you are importing your pipe in incorrect way, you have to import your pipe in the module instead of your component ie orders component in your use case. 最有可能的是,您正在为应用程序使用ngModule方法,而不是以不正确的方式导入管道,而必须在模块而不是组件中导入管道,即在用例中订购组件。

Try importing your pipe in higher module like this: 尝试像这样在更高的模块中导入管道:

@NgModule({
  declarations: [FilterPipe,.... ],
  imports: [.... ],
  providers: [....],
  bootstrap: [AppComponent]
})
export class AppModule { }

PS:- Moreover, you can also create your pipe as module to import this in more than one modules. PS:-此外,您还可以将管道创建为模块,以将其导入多个模块中。

如果使用的是角度4,则无需在@Component中指定管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM