Angular2 管道 - 意外标记 <(..)

Angular2 pipes - Unexpected token <(..)

我想在我的 Angular 2 项目中添加用于搜索的管道,但问题是,当我将创建的管道包含到组件浏览器日志中时,我会看到意外的令牌 <(..) 错误。 完整日志:

    angular2-polyfills.js:332 
Error: SyntaxError: Unexpected token <(…)
ZoneDelegate.invoke @ angular2-polyfills.js:332Zone.run @ angular2-polyfills.js:227(anonymous function) @ angular2-polyfills.js:576Z
oneDelegate.invokeTask @ angular2-polyfills.js:365Zone.runTask @ angular2-polyfills.js:263drainMicroTaskQueue @ angular2-polyfills.js:482ZoneTask.invoke @ angular2-polyfills.js:434

这是我的管道代码:

    import {Pipe} from 'angular2/core';

@Pipe({
  name: 'SearchPipe'
})

export class SearchPipe {

   transform(items: any[], args: any[]): any {
        return items.filter(item => item.name.indexOf(args[0]) != -1);
   }
}

这里是组件代码。

 import { Component, Directive } from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {SearchPipe} from 'pipe/searchpipe';
import 'rxjs/Rx';


@Component({ 
  pipes: [SearchPipe],
  selector: 'MainPage', 
  templateUrl: 'app/mainpage/mainpage.html'
})

export class MainPageComponent { 
    korisnici: Object[];
    constructor(http: Http){
        http.get('korisnici.json')
        .map(res => res.json())
        .subscribe(korisnici => this.korisnici = korisnici);
    }
}

我上次在没有添加 http.dev.js 库的情况下使用 http 时发生了这些错误。是否有一些用于包含管道的 JS 库,或者我在其他方面有误?

Pipe 类型包含在 angular2/core 中,因此无需再添加。

话虽如此,导入时可能出现问题 pipe/searchpipe。您需要检查从您要使用它的地方到达该模块的路径。

大多数情况下,错误 Unexpected token <(…) 在加载模块时发生在 404 上。

一些关于管道的 Anglar 2 信息目前不正确。

Angular 2 版本删除了 'pipes' 中的声明: @Component({ pipes: [YourPipe], ... })

并将其移至 (app.module.ts): @NgModule({ ... declarations: [ YourPipe ], ... })