我正在尝试将来自 rxjs 6 的计时器放在 angular 6 服务中。而且它没有被触发。我查看了文档,没有任何运气。这是我的服务代码(仅相关部分:
import { map,flatMap, catchError, take } from 'rxjs/operators';
import { BehaviorSubject, of, throwError,timer, Observable } from "rxjs";
.....
countdownTimer = new Observable<number>();
formatCountdownTime(count) {
const seconds = count % 60 < 10 ? '0' + Math.floor(count % 60) : Math.floor(count % 60);
/* if(count <= 1000){
//set timer to NOT RUNNING state so it updates UI components like the button that depends on the count
this.contributorManagerService.countdownTimerIsRunning.next(false);
} */
return Math.floor(count / 60) + ':' + seconds;
}
createCountdownTimerObservable(count){
this.countdownTimer = timer(0, 1000);
}
这是计时器的消耗。我需要知道时间何时过去,这就是我将第 3 个函数参数传递给订阅的原因,因为我需要在时间结束时启用按钮。
import { map,take } from 'rxjs/operators';
export class CampaignDataComponent implements OnInit, OnDestroy {
countdownTimerIsRunning = false ;
count: number;
ngOnInit(){
/* I subscribe to the timer and the 3rd parameter indicates what to do when time has elapsed */
this.sharedHelpersService.countdownTimer.
pipe(
take(this.count),
map(() => {
--this.count;
return this.count;
})
).subscribe(count=>{
console.log("New count",count);
if(count>0){
this.countdownTimerIsRunning = true;
}
},
err=>{
console.log("Countdown timer error", err);
},
()=>{
console.log("Time has elapsed");
this.countdownTimerIsRunning = false;
});
}
}
你知道为什么没有被触发吗?当我在组件上使用整个链时,它曾经工作过,但由于我需要从其他组件中使用它,我不得不将其放入服务中,这破坏了它。有什么想法吗?提前非常感谢
编辑:澄清一下,它们所有组件都应该使用相同的倒计时
以下方法应该可以使计时器由多个组件共享:
服务:
import { Injectable } from '@angular/core';
import { timer, Subject, Observable } from 'rxjs';
import { takeWhile, map } from 'rxjs/operators';
@Injectable()
export class CountdownService {
private _countdown = new Subject<number>();
countdown(): Observable<number> {
return this._countdown.asObservable();
}
private isCounting = false;
start(count: number): void {
// Ensure that only one timer is in progress at any given time.
if (!this.isCounting) {
this.isCounting = true;
timer(0, 1000).pipe(
takeWhile(t => t < count),
map(t => count - t)
).subscribe(
t => this._countdown.next(t),
null,
() => {
this._countdown.complete();
this.isCounting = false;
// Reset the countdown Subject so that a
// countdown can be performed more than once.
this._countdown = new Subject<number>();
}
);
}
}
}
组件可以通过以下方式启动倒计时
countdownService.start(myCountdownTime)
所有对倒计时时间感兴趣的组件都可以订阅
countdownService.countdown().subscribe(t => ...)
堆栈闪电战示例
问题出在这段代码上
pipe(
take(this.count),
map(() => {
--this.count;
return this.count;
})
我假设组件中没有其他代码,因此 this.count 初始化为 0。 然后你订阅可观察量并说有效 take(0(,这样可观察量立即完成。
当您说需要使用其他组件的倒计时时,如果您的意思是您需要使用其他组件的功能,但订阅的每个组件都有自己独立的倒计时,那么您可以执行以下操作:
服务:
import { timer, Observable } from 'rxjs';
import { filter, takeWhile, map } from 'rxjs/operators';
// startTime is in seconds
count(startTime: number): Observable<number> {
return timer(0, 1000).pipe(
takeWhile(t => t < startTime),
map(t => startTime - t)
)
}
元件:
// Count down for 10 seconds.
countdownService.count(10).subscribe(
t => console.log(t),
null,
() => console.log('Done!')
)
这将打印:10 9 8 7 6 5 4 3 2 1 完成!