打字稿rxjs:setimeout 0是否有任何替代方法

时间:2020-03-04 11:17:06

标签: javascript angular typescript ecmascript-6 rxjs

有了我的角度应用程序,我正在使用这种治疗方法

obj

我被告知:myMethod(){ ... setTimeout(() => { this.router.navigate(['mucomponent']); }); } 没有延迟(0)似乎在等待所有治疗完成的下一刻开始我的治疗。这是不久的将来的计划。

因为我需要这种行为

是否可以使用 typescript rxjs 做到这一点呢?

建议?

3 个答案:

答案 0 :(得分:2)

这应该可行,并且比以前的解决方案更简单:

timer(0).subscribe(time => this.router.navigate(['mucomponent']));

timer() 有两个参数,第二个是可选的。如果提供2个参数,第一个参数是延迟,第二个参数是周期。如果只提供一个参数,就是延迟,定时器只会发出1个值(不会重复)。

来自 timer() 的文档:

<块引用>

如果没有指定 period(第二个参数),输出 Observable 只发出一个 值,0。

参考: https://rxjs-dev.firebaseapp.com/api/index/function/timer

答案 1 :(得分:1)

您可以使用Interval或使用rxjs;

import { interval } from 'rxjs';
import { take} from 'rxjs/operators';

myMethod() {
  interval(0).pipe(take(1),
   ).subscribe(value =>
    this.router.navigate(['mucomponent']);
}

答案 2 :(得分:0)

如果您正在尝试某种调度程序,这是RxJS超时

https://rxjs-dev.firebaseapp.com/api/operators/timeout