我已经为格式化数据创建了下面的服务。从角组件调用服务,guest.vo.ts返回处理过的数据。
guest.vo.ts
import { Guest } from '../core/guest/guest.model';
import { TimeAgoPipe } from 'angular2-moment/time-ago.pipe';
import { Injectable } from '@angular/core';
@Injectable()
export default class GuestVO {
constructor(private timeAgo: TimeAgoPipe) {}
getVO(guests: Guest[]) {
return guests.map(guest => {
let vo: any = {};
vo.created = this.timeAgo.transform(guest.created);
return vo;
});
}
}我正在使用下面的库进行时间计算https://github.com/urish/angular2-moment/blob/master/src/time-ago.pipe.ts
但是,我在服务中注入TimeAgoPipe时遇到了错误,因为它依赖于ChangeDetectorRef。
如果我直接在组件中使用TimeAgoPipe,则不会出现此问题。对使用这个管道有什么建议吗?
constructor(
private cdRef: ChangeDetectorRef,
private ngZone: NgZone) {
}发布于 2018-02-23 16:19:00
管子是不可注射的。您只需将其添加到模块的declarations中即可使用。如果您想注入一个管道,您可以将它作为一个服务提供,例如
{ provide: MyPipe, useClass: TimeAgoPipe }并将它的实例作为MyPipe注入
https://stackoverflow.com/questions/48952079
复制相似问题