我有用户服务,该服务仅获取或更新服务器上的数据并将其缓存。使用shareReplay一切正常:
Private Sub btnGenerateEmail_Click()
Dim obApp As Object
Dim objMsg As MailItem
Set obApp = Outlook.Application
Set objMsg = obApp.CreateItem(olMailItem)
With objMsg
.To = "test@test.com"
.cc = "test@test.com"
.Subject = "Scrap Face Incident Report"
.Display
End With
End Sub
不同的组件像
一样订阅thisObservableprivate cache$: Observable<User>;
get user() {
if (!this.cache$) {
this.cache$ = this.getUser().pipe(shareReplay(1))
}
return this.cache$;
}
我面临通过服务更新一个组件中的用户之后的问题
this.userService.user.subscribe(user => this.user)
我需要更新this.userService.updateUser(user).subscribe()
中的值。我发现最好将cache$
用于此类目的。因此,我相应地更新了我的服务。
ReplaySubject
我更新值,例如
private cache$: ReplaySubject<User> = new ReplaySubject<User>(CACHE_SIZE);
private cacheHasData = false;
get user() {
if (!this.cacheHasData) {
this.getUser().subscribe(userResponse => {
this.cache$.next(userResponse);
this.cacheHasData = true;
});
}
return this.cache$;
}
一切都正常,除了环形部分。每次加载其他组件时,都会调用Http请求。为什么会这样?
P.S 我使用了Correct use of Angular 6 ReplaySubject for caching - observers length keeps growing中的示例 但是在链接的主题中,作者将Observable传递给.next(),所以我认为这不适合我的情况。