【发布时间】:2020-06-18 11:39:28
【问题描述】:
我正在尝试创建一种在解析某些流时动态呈现和隐藏加载屏幕的方法。
这是我当前的代码:
this.requestService
.get<Checkout>(`check/${orderNumber}`)
.pipe(
tap(() => this.startLoading()), //i think this isnt the right way to use this pipe
finalize(() => this.endLoading())
)
.subscribe(
data => {
data.orderNumber = orderNumber
this.checkout.next(data)
},
error => {
this.notification.warning(error)
}
)
预期的结果是当我的流开始时,以startLoading() 显示加载屏幕,在操作完成时结束,使用endLoading() 隐藏加载。
我的工作代码:
this.startLoading() //basically calling function before i create the stream
this.requestService
.get<Checkout>(`check/${orderNumber}`)
.pipe(
finalize(() => this.endLoading())
)
.subscribe(
data => {
data.orderNumber = orderNumber
this.checkout.next(data)
},
error => {
this.notification.warning(error)
}
)
我正确使用了这个tap 管道吗?是否有其他管道可以更好地解决此问题?
使用 RxJS 最好的方法是什么?
【问题讨论】:
-
使用第一个示例我的
startLoading()函数在流启动时不会被调用 -
您希望在全局范围内还是在一个组件中执行此操作?
-
在角度服务中,但我不知道它是否重要
标签: angular typescript rxjs stream reactive-programming