有一个伟大的管道张贴在这里。
我正在尝试使其与 Ionic 2 RC 版本一起使用并出现以下三个错误:
- 类型 'any' 不是构造函数类型
- 找不到名称“ChangeDetectorRef”。导出类的构造函数的参数“ref”具有或正在使用私有名称“ChangeDetectorRef”。
“typeof Observable”类型上不存在属性“interval”
import {Pipe, ChangeDetectorRef} from 'angular2/core'; import {Observable} from 'rxjs/Observable'; import {AsyncPipe} from 'angular2/common'; @Pipe({ name: 'messageTime', pure: false }) export class MessageTimePipe extends AsyncPipe { //Type 'any' is not a constructor function type value:Date; timer:Observable<string>; constructor(ref:ChangeDetectorRef) { //Cannot find name 'ChangeDetectorRef'. Parameter 'ref' of constructor from exported class has or is using private name 'ChangeDetectorRef'. super(ref); } transform(obj:any, args?:any[]):any { if (obj instanceof Date) { this.value = obj; if(!this.timer) { this.timer = this.getObservable(); } return super.transform(this.timer, args); } return super.transform(obj, args); } private getObservable() { return Observable.interval(1000).startWith(0).map( () => { //Property 'interval' does not exist on type 'typeof Observable' var result:string; // current time let now = new Date().getTime(); // time since message was sent in seconds let delta = (now - this.value.getTime()) / 1000; // format string if (delta < 10) { result = 'jetzt'; } else if (delta < 60) { // sent in last minute result = 'vor ' + Math.floor(delta) + ' Sekunden'; } else if (delta < 3600) { // sent in last hour result = 'vor ' + Math.floor(delta / 60) + ' Minuten'; } else if (delta < 86400) { // sent on last day result = 'vor ' + Math.floor(delta / 3600) + ' Stunden'; } else { // sent more than one day ago result = 'vor ' + Math.floor(delta / 86400) + ' Tagen'; } return result; }); };
}
关于如何解决这个问题的任何想法?
编辑:我能够通过使用消除错误 3/import {Observable} from 'rxjs/Rx';