RxjS shareReplay:如何重置其值? [英] RxjS shareReplay : how to reset its value?
本文介绍了RxjS shareReplay:如何重置其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 shareReplay
仅调用一次 (如缓存)网络服务来检索一些信息:
I use shareReplay
to call only once (like a cache) a webservice to retrieve some informations :
在我的服务中:
getProfile(): Observable<Customer> {
return this.callWS().pipe(shareReplay(1));
}
在多个组件中:
this.myService.getProfile().subscribe(customer => {
console.log('customer informations has been retrieved from WS :', customer);
});
现在,我想添加一种强制刷新信息的方法(仅绕过一次shareReplay)。我尝试将我的observable存储在一个变量中,然后在将其初始化之前将其设置为null,但这似乎破坏了组件的订阅。.
Now I want to add a method to force refresh the informations (bypass shareReplay only once). I tried with storing my observable in a variable, and set it to null before re-initialize it, but it seems to break components subscriptions..
有什么帮助吗?
谢谢
推荐答案
感谢马丁的回答(不是
protected profile: Observable<Customer>;
private refresh$ = new Subject<Customer>();
constructor() {
this.profile = this.refresh$.pipe(shareReplay(1));
this.resetProfile();
}
getProfile(): Observable<Customer> {
return this.profile;
}
resetProfile() {
this.callWS().subscribe(user => {
this.refresh$.next(user);
});
}
我认为可能有更好/更清洁的方法(使用行为主体) ?),所以,如果您知道更好的话..
I think there is maybe something better/cleaner to do (using a Behavior Subject ?), so if you know something better ..
这篇关于RxjS shareReplay:如何重置其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文