【发布时间】:2016-03-27 13:50:54
【问题描述】:
我想在我的 Angular 2 项目中添加用于搜索的管道,但问题是当我将创建的管道包含到组件浏览器日志中时,会显示 Unexpected token
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 时没有添加 http.dev.js 库。是否有一些用于包含管道的 JS 库或者我在其他方面错了?
【问题讨论】:
-
你确定
SearchPipe所在的文件是searchpipe吗? -
是的,我现在已将管道目录移动到 app/pipe,现在它已加载,但现在我收到消息 TypeError: Cannot read property 'filter' of null..
标签: angular angular2-routing angular2-directives