简体   繁体   English

Angular2:在组件上找不到管道装饰器

[英]Angular2: No Pipe decorator found on Component

I am using a few pretty standard custom pipes in my Angular 2.0-rc1 app.我在我的 Angular 2.0-rc1 应用程序中使用了一些非常标准的自定义管道。 All was well until I moved the pipes to a new folder.一切都很好,直到我将管道移动到一个新文件夹。 Now I am getting:现在我得到:

zone.js:461 Unhandled Promise rejection: No Pipe decorator found on RoomDetailWidgetComponent ; zone.js:461 未处理的承诺拒绝:在 RoomDetailWidgetComponent 上找不到管道装饰器; Zone: angular ;区域:角度; Task: Promise.then ;任务: Promise.then ; Value: BaseException {message: "No Pipe decorator found on RoomDetailWidgetComponent", stack: "Error: No Pipe decorator found on RoomDetailWidget…/@angular/compiler/src/runtime_compiler.js:66:49)"}message: "No Pipe decorator found on RoomDetailWidgetComponent"stack: "Error: No Pipe decorator found on RoomDetailWidgetComponent↵ at new BaseException ( http://localhost:5000/lib/@angular/compiler/src/facade/exceptions.js:17:23 )↵ at PipeResolver.resolve ( http://localhost:5000/lib/@angular/compiler/src/pipe_resolver.js:29:15 )↵ at CompileMetadataResolver.getPipeMetadata ( http://localhost:5000/lib/@angular/compiler/src/metadata_resolver.js:142:47 )↵ at eval ( http://localhost:5000/lib/@angular/compiler/src/metadata_resolver.js:173:57 )↵ at Array.map (native)↵ at CompileMetadataResolver.getViewPipesMetadata ( http://localhost:5000/lib/@angular/compiler/src/metadata_resolver.js:173:22 )↵ at eval ( http://localhost:5000/lib/@angular/compiler/src/runtime_compiler.js:80:58 )↵ at Array.forEach (值:BaseException {message: "No Pipe decorator found on RoomDetailWidgetComponent", stack: "Error: No Pipe decorator found on RoomDetailWidget.../@angular/compiler/src/runtime_compiler.js:66:49)"}message: "No Pipe在 RoomDetailWidgetComponent 上找到装饰器“堆栈:”错误:在新的 BaseException ( http://localhost:5000/lib/@angular/compiler/src/facade/exceptions.js:17:23 )↵ 处未在 RoomDetailWidgetComponent↵ 上找到管道装饰器PipeResolver.resolve ( http://localhost:5000/lib/@angular/compiler/src/pipe_resolver.js:29:15 )↵ 在 CompileMetadataResolver.getPipeMetadata ( http://localhost:5000/lib/@angular/compiler/ src/metadata_resolver.js:142:47 )↵ at eval ( http://localhost:5000/lib/@angular/compiler/src/metadata_resolver.js:173:57 )↵ at Array.map (native)↵ at CompileMetadataResolver .getViewPipesMetadata ( http://localhost:5000/lib/@angular/compiler/src/metadata_resolver.js:173:22 )↵ at eval ( http://localhost:5000/lib/@angular/compiler/src/runtime_compiler .js:80:58 )↵ 在 Array.forEach ( native)↵ at RuntimeCompiler._compileComponent ( http://localhost:5000/lib/@angular/compiler/src/runtime_compiler.js:76:36 )↵ at eval ( http://localhost:5000/lib/@angular/compiler/src/runtime_compiler.js:66:49 )" proto : ErrorconsoleError @ zone.js:461_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426 zone.js:463 Error: Uncaught (in promise): No Pipe decorator found on RoomDetailWidgetComponent(…)consoleError @ zone.js:463_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426 native)↵ at RuntimeCompiler._compileComponent ( http://localhost:5000/lib/@angular/compiler/src/runtime_compiler.js:76:36 )↵ at eval ( http://localhost:5000/lib/@angular/编译器/src/runtime_compiler.js:66:49)" proto :ErrorconsoleError @ zone.js:461_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426 zone.js:463 错误:未捕获(承诺):在 RoomDetailWidgetComponent(...)consoleError @ zone.js:463_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426 上找不到管道装饰器

 import { Component, AfterViewInit, Input, SimpleChange, Output, EventEmitter } from "@angular/core"; import { IPropertyRoom } from "./../../shared/propertyData"; import { FirstHalfPipe } from "./../../shared/pipes/first-half.pipe"; import { SecondHalfPipe } from "./../../shared/pipes/second-half.pipe"; export interface IRoomData { roomId: number; isFeatured: boolean; roomName: string; featurePicSrc: string; description: string; amenities: string[]; } @Component({ selector: "room-detail-widget", templateUrl: "app/mobile/roomDetailWidget/room-detail-widget.html", directives: [], pipes: [FirstHalfPipe,SecondHalfPipe] }) export class RoomDetailWidgetComponent { @Input() roomDetail: IRoomData; @Output() onRoomTabClick = new EventEmitter(); constructor() { } ngOnChanges(changes: { [propName: string]: SimpleChange }) { this.roomDetail = changes["roomDetail"].currentValue; } roomClick() { this.onRoomTabClick.emit(this.roomDetail.roomId); } }

As you can see, I am declaring the pipes in my component.如您所见,我在组件中声明了管道。 The paths are all ok.路径一切正常。 Can't figure out how to fix this.无法弄清楚如何解决这个问题。

As it turned out it was a "dumb" error. 事实证明,这是一个“愚蠢”的错误。

I accidentally referenced the offending component as a pipe, rather than a directive, in the parent component as follows: 我在父组件中意外地将有问题的组件引用为管道而不是指令,如下所示:

 import { RoomDetailWidgetComponent, IRoomData } from "./../roomDetailWidget/room-detail-widget.component" @Component({ directives: [], selector: "parent", templateUrl: "path/parent.html", pipes: [RoomDetailWidgetComponent] //Duh, big FAIL }) export class ParentComponent { constructor() { } } 

Hopefully this will help avert headache for somebody else. 希望这将有助于避免其他人头痛。 Easy mistake to make... 容易犯的错误...

Pipes don't support inheritance. 管道不支持继承。 It's just a guess but I remember seeing that a pipe that extends another class causes this error. 只是一个猜测,但我记得看到扩展另一个类的管道会导致此错误。 https://github.com/angular/angular/issues/8694 https://github.com/angular/angular/issues/8694

It seems that Angular2 can find metadata for a pipe. 看来Angular2可以找到管道的元数据。

When you add the Pipe decorator, the following is added. 添加Pipe装饰器时,将添加以下内容。

[PipeMetadata]
  0: PipeMetadata
    _pure: undefined
    name: "some"
    pure: (...)
    __proto__: InjectableMetadata
  length:1
  __proto__: Array[0]

See this plunkr: https://plnkr.co/edit/d0gXbGm3mus1cQLE1wgB?p=preview . 请参阅以下代码: https ://plnkr.co/edit/d0gXbGm3mus1cQLE1wgB ? p = preview。

Perhaps your path to modules containing the pipes aren't correct so your imported pipes are null... 可能您通往包含管道的模块的路径不正确,因此导入的管道为空...

I have used TimeAgoPipe https://www.npmjs.com/package/time-ago-pipe in Angular10 Project.我在 Angular10 项目中使用了 TimeAgoPipe https://www.npmjs.com/package/time-ago-pipe In my case, "TimeAgoPipe" is only used in the Angular2 project.就我而言,“TimeAgoPipe”仅用于 Angular2 项目。

So for Angular10 we need to use "ngx-timeago"所以对于 Angular10 我们需要使用“ngx-timeago”

Please refer this: https://www.npmjs.com/package/ngx-timeago请参考: https : //www.npmjs.com/package/ngx-timeago

Short snippet:短片:

Package-json: "ngx-timeago":"^2.0.0"包-json:“ngx-timeago”:“^2.0.0”

App.module.ts: inside imports array => importsTimeagoModule.forRoot() App.module.ts:在导入数组中=>importsTimeagoModule.forRoot()

Shared.module.ts: inside Exports array add => TimeagoModule Shared.module.ts: 在 Exports 数组中添加 => TimeagoModule

testpipe.html: "{{date | timeago}}" testpipe.html: "{{date | timeago}}"

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

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