Ionic RC - Great Angular 2 pipe' timeAgo'

时间:2016-10-24 11:45:04

标签: angular ionic2 angular-pipe

here张贴了一个很棒的管道。

我试图让它与Ionic 2 RC版本配合使用并获得这三个错误:

  1. 键入'任何'不是构造函数类型
  2. 找不到名称' ChangeDetectorRef'。参数' ref'导出类中的构造函数具有或正在使用私有名称' ChangeDetectorRef'。
  3. 财产'间隔'类型' typeof Observable'

    上不存在
    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;
        });
    };
    

    }

  4. 关于如何解决这个问题的任何想法?

    编辑:我可以使用import {Observable} from 'rxjs/Rx';

    取出错误3 /

2 个答案:

答案 0 :(得分:0)

使用angular 2 moment为什么需要创建已经存在的东西?

答案 1 :(得分:0)

对于rxjs 6。*

DECLARE @FromDate DATETIME = '2018-06-14 12:23:12.661'
DECLARE @HoursToAdd INT = 15

;WITH RecursiveHourSplit AS
(
    SELECT
        StartDateTime = W.StartDateTime,
        EndDateTime = W.EndDateTime,
        HourSplitDateTime = W.StartDateTime
    FROM
        #WorkingCalendar AS W

    UNION ALL

    SELECT
        StartDateTime = W.StartDateTime,
        EndDateTime = W.EndDateTime,
        HourSplitDateTime = DATEADD(HOUR, 1, W.HourSplitDateTime)
    FROM
        RecursiveHourSplit AS W
    WHERE
        DATEADD(HOUR, 1, W.HourSplitDateTime) < W.EndDateTime
),
HourRowNumber AS
(
    SELECT
        R.HourSplitDateTime,
        RowNumber = ROW_NUMBER() OVER (ORDER BY R.HourSplitDateTime ASC)
    FROM
        RecursiveHourSplit AS R
    WHERE
        @FromDate < R.HourSplitDateTime
)
SELECT
    DATETIMEFROMPARTS(
        YEAR(R.HourSplitDateTime),
        MONTH(R.HourSplitDateTime),
        DAY(R.HourSplitDateTime),
        DATEPART(HOUR, R.HourSplitDateTime),

        DATEPART(MINUTE, @FromDate),
        DATEPART(SECOND, @FromDate),
        DATEPART(MILLISECOND, @FromDate))
FROM
    HourRowNumber AS R
WHERE
    R.RowNumber = @HoursToAdd