您好我想找一个倒数计时器的例子,我发现在Stack上找到了这里:Time CountDown in angular 2
这是我的代码:
import { Component, ElementRef, OnInit, OnDestroy } from '@angular/core';
import { Observable, Subscription, interval } from 'rxjs';
@Component({
selector: 'app-timer',
templateUrl: './timer.component.html',
styleUrls: ['./timer.component.css']
})
export class TimerComponent implements OnInit {
private future: Date;
private futureString: string;
private diff: number;
private $counter: Observable<number>;
private subscription: Subscription;
private message: string;
constructor(elm: ElementRef) {
this.futureString = elm.nativeElement.getAttribute('inputDate');
}
dhms(t) {
let days, hours, minutes, seconds;
days = Math.floor(t / 86400);
t -= days * 86400;
hours = Math.floor(t / 3600) % 24;
t -= hours * 3600;
minutes = Math.floor(t / 60) % 60;
t -= minutes * 60;
seconds = t % 60;
return [
days + 'd',
hours + 'h',
minutes + 'm',
seconds + 's'
].join(' ');
}
ngOnInit() {
this.future = new Date(this.futureString);
this.$counter = Observable.interval(1000).map((x) => {
this.diff = Math.floor((this.future.getTime() - new Date().getTime()) / 1000);
return x;
});
this.subscription = this.$counter
.subscribe((x) => this.message = this.dhms(this.diff));
}
}
收到以下错误:
timer / timer.component.ts(44,34):错误TS2339:属性&#39; interval&#39; 类型&#39; typeof Observable&#39;。
上不存在
我已尝试过在Google上可以找到的所有导入措施,但没有任何效果。我也更新到最新版本的rxjs,但仍然没有。任何帮助将不胜感激。
我相信我可能会遇到某种版本问题。真的很难过。
npm ERR! peer dep missing: jquery@1.9.1 - 3, required by bootstrap@4.1.1 npm ERR! peer dep missing: popper.js@^1.14.3, required by bootstrap@4.1.1 npm ERR! missing: mkdirp@0.5.1, required by node-pre-gyp@0.9.1 npm ERR! missing: minimist@0.0.8, required by mkdirp@0.5.1 npm ERR! missing: minimatch@3.0.4, required by ignore-walk@3.0.1 npm ERR! missing: brace-expansion@1.1.11, required by minimatch@3.0.4 npm ERR! missing: balanced-match@1.0.0, required by brace-expansion@1.1.11 npm ERR! missing: concat-map@0.0.1, required by brace-expansion@1.1.11 npm ERR! missing: console-control-strings@1.1.0, required by npmlog@4.1.2 npm ERR! missing: inherits@2.0.3, required by readable-stream@2.3.6 npm ERR! missing: safe-buffer@5.1.1, required by readable-stream@2.3.6 npm ERR! missing: safe-buffer@5.1.1, required by string_decoder@1.1.1 npm ERR! missing: console-control-strings@1.1.0, required by gauge@2.7.4 npm ERR! missing: string-width@1.0.2, required by gauge@2.7.4 npm ERR! missing: strip-ansi@3.0.1, required by gauge@2.7.4 npm ERR! missing: code-point-at@1.1.0, required by string-width@1.0.2 npm ERR! missing: is-fullwidth-code-point@1.0.0, required by string-width@1.0.2 npm ERR! missing: strip-ansi@3.0.1, required by string-width@1.0.2 npm ERR! missing: number-is-nan@1.0.1, required by is-fullwidth-code-point@1.0.0 npm ERR! missing: ansi-regex@2.1.1, required by strip-ansi@3.0.1 npm ERR! missing: string-width@1.0.2, required by wide-align@1.1.2 npm ERR! missing: inherits@2.0.3, required by glob@7.1.2 npm ERR! missing: minimatch@3.0.4, required by glob@7.1.2 npm ERR! missing: once@1.4.0, required by glob@7.1.2 npm ERR! missing: once@1.4.0, required by inflight@1.0.6 npm ERR! missing: wrappy@1.0.2, required by inflight@1.0.6 npm ERR! missing: wrappy@1.0.2, required by once@1.4.0 npm ERR! missing: minipass@2.2.4, required by tar@4.4.1 npm ERR! missing: mkdirp@0.5.1, required by tar@4.4.1 npm ERR! missing: safe-buffer@5.1.1, required by tar@4.4.1 npm ERR! missing: yallist@3.0.2, required by tar@4.4.1 npm ERR! missing: minipass@2.2.4, required by fs-minipass@1.2.5 npm ERR! missing: safe-buffer@5.1.1, required by minipass@2.2.4 npm ERR! missing: yallist@3.0.2, required by minipass@2.2.4 npm ERR! missing: minipass@2.2.4, required by minizlib@1.1.0
答案 0 :(得分:10)
简单地写一下:
import { interval } from 'rxjs';
import { map } from 'rxjs/operators'
interval(1000).pipe(
map((x) => { /* your code here */ })
);
在RxJS 6+中,没有Observable.interval
函数。
答案 1 :(得分:1)
从Rxjs 6.0
开始,您必须从interval
导入rxjs/observable/interval
。
您必须使用pipe
运算符按顺序执行无限数量的运算符。
import { interval } from 'rxjs';
import { map } from 'rxjs/operators'
this.$counter = interval(1000).pipe(
map((x) => {
this.diff = Math.floor((this.future.getTime() - new Date().getTime()) / 1000);
return x;
});
)
答案 2 :(得分:0)
ngx-countdown是更成熟的解决方案。它提供了所有格式选项以及开始/停止/暂停/继续功能。
现场演示here