我正在尝试在加载 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 评论