我正在 Angular 应用程序中实现缓存 HTTP 结果。据我所知,以下代码均有效,但我需要知道它们是否正在执行exactly同样的事情,或者我错过了一些重要的事情?
发布最后
getPosts() {
if( !this.posts$ ) {
this.posts$ = this.http.get('api').publishLast().refCount();
return this.posts$;
}
return this.posts$;
}
发布重播
getPosts() {
if( !this.posts$ ) {
this.posts$ = this.http.get('api').publishReplay(1).refCount();
return this.posts$;
}
return this.posts$;
}
publishLast
共享(顾名思义)last发射值 - 只能在流传输时确定完成.
publishReplay(1)
分享latest发射值,这是在任何发射之后完成的。
如果是this.http.get(...)
行为是相同的,因为流将在收到结果后完成,因此last和latest值是同一个东西。
但是,对于发出多个值或在发出该值后未立即完成的流,您将得到不同的结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)