拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 当angular条件为真时取消订阅observable

当angular条件为真时取消订阅observable

白鹭 - 2022-01-26 2139 0 0

我正在尝试在加载 5 秒后关闭弹出视窗,但订阅在此之前完成,所以我想在加载的变量为 true 时取消订阅

    this.closed = false;
    this.loaded$.subscribe(loaded => {
      if (loaded) {
        setTimeout(() => {
          this.modalService.dismissActiveModal();
        }, 5000);
      }
    }).unsubscribe();
  } 

uj5u.com热心网友回复:

this.closed = false;
// define the subscription
const subscription: Subscription = this.loaded$.subscribe(loaded => {
  if (loaded) {
    setTimeout(() => {
      this.modalService.dismissActiveModal();
      subscription.unsubscribe(); // unsubscribe here
    }, 5000);
  }
});

这个怎么样?

uj5u.com热心网友回复:

this.closed = false;
this.loaded$
.pipe(
  filter(loaded => loaded),
  switchMap(_ => interval(5000)),
  take(1),
  tap(_ => this.modalService.dismissActiveModal())
)
.subscribe();
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *