如何解决加载微调框不会出现延迟
正在加载加载微调器,并在延迟后消失,但它根本没有出现,但是如果我删除“ this.isLoading = false”行,则微调器会显示但不会消失!
const myObservable = of("foobar");
myObservable.subscribe((value) => {
this.isLoading = true,delay(5000),// If i remove the line below the spinner is showing
this.isLoading = false
console.log(value);
});
解决方法
delay()是rxjs运算符。用法如下:
of("foobar").pipe(
tap(_ => {this.isLoading = true}),delay(5000),tap(_ => {this.isLoading = false}),).subscript()
,
我会说最好使用tap
和finalize
运算符来控制微调框的可见性,而不是固定的时间。
尝试以下
控制器
isLoading = false;
myObservable.pipe(
tap(_ => this.isLoading = true),// <-- triggered as soon the subscription is invoked
finalize(_ => this.isLoading = false) // <-- triggered when the observable completes
).subscribe((value) => {
console.log(value);
});
模板
<ng-container *ngIf="isLoading">
<!-- show spinner -->
</ng-container>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。