Cal*_*röm 1 rxjs ngrx angular-pipe angular ngrx-store
给定一个看起来像这样的模板
<some-component
*ngIf="someColdObservable$ | async"
[result]="someColdObservable$ | async"
></some-component>
Run Code Online (Sandbox Code Playgroud)
和一个看起来像这样的 observable:
someColdObservable$: this.store.pipe(
select(isAllowedToDoThis),
filter(Boolean),
flatMap(() => apiRequest())
);
Run Code Online (Sandbox Code Playgroud)
该someColdObservable$
被订阅两次(如预期),这反过来问题的两个API调用(这显然是一个代码味道,但让我们无视此刻)。
在这种情况下some-component
不包含任何空检查,AsyncPipe
如果在模板中评估apiRequest
之前没有发出值AsyncPipe
导致some-component
抛出cannot access x of null
(此时[result]
仍然为空),则将返回空值,请参阅AsyncPipe
参考源。
这是所有预期的行为(或至少在阅读源代码之后)但是,当我尝试通过添加shareReplay
to来缓解发出两个请求someColdObservable$
的问题时,我还解决了[result]
在apiRequest()
发出值之前为 null的问题。这对我来说毫无意义,因为我希望AsyncPipe
仍然在null
_latestValue
这里返回一个,而使cannot access x of null
错误未修复。但出于某种原因,添加shareReplay
修复了上述两个问题。
这类似于共享时的 Angular 可观察行为奇怪,但仍然存在未解决的问题,即为什么要shareReplay
解决此问题。
有人能指出我在这里缺少什么以及为什么在发出值之前AsyncPipe
不再返回吗?null
apiRequest()
感谢任何指针和输入,谢谢!
第一个场景:
第二种情况:
比使用 shareReplay 更好的解决方案是将异步管道的结果存储在变量中并使用它:
<some-component
*ngIf="someColdObservable$ | async as result"
[result]="result"
></some-component>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1055 次 |
最近记录: |