简体   繁体   English

Angular2管道-意外令牌<(..)

[英]Angular2 pipes - Unexpected token <(..)

I wanted to add pipe for search in my Angular 2 project but the problem is that when i include the created pipe into component browser logs shows me Unexpected token <(..) error. 我想在我的Angular 2项目中添加用于搜索的管道,但是问题是当我将创建的管道包含在组件浏览器日志中时,显示我意外的令牌<(..)错误。 Full log: 完整日志:

    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

Here is my pipe code: 这是我的管道代码:

    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);
   }
}

And here is component code. 这是组件代码。

 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);
    }
}

These error happened for me last time when i was using http without adding http.dev.js library. 这些错误是我上次使用http而不添加http.dev.js库时发生的。 Is there some JS library for including pipes or i am wrong on something else? 是否有一些包含管道的JS库,或者我在其他方面错了?

The Pipe type is included into angular2/core so there is nothing more to add. Pipe类型包含在angular2/core因此无需添加其他内容。

That being said, perhaps there is a problem when importing pipe/searchpipe . 话虽如此,也许在导入pipe/searchpipe时出现问题。 You need to check the path to reach this module from where you want to use it. 您需要检查从其使用位置到达此模块的路径。

Most of time the error Unexpected token <(…) occurs on a 404 when loading a module. 多数情况下,加载模块时,404上会出现错误的Unexpected token <(…)

Some of the Anglar 2 information on pipes is currently incorrect. 管道上的某些Anglar 2信息当前不正确。

Angular 2 release removed the 'pipes' declaration in: @Component({ pipes: [YourPipe], ... }) Angular 2版本删除了以下内容中的“管道”声明: @Component({ pipes: [YourPipe], ... })

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

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

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