我有一个rxjs @ 6 BehaviorSubject source$
,我想从中获取子值source$
const source$ = new BehaviorSubject(someValue);
const subSource$ = source$.pipe(map(transform));
Run Code Online (Sandbox Code Playgroud)
我期望the subSource$
也是一个BehaviorSubject,但不是,我如何才能获得subSource$
一个BehaviorSubject?
小智 3
当BehaviorSubject通过管道传输时,它使用AnonymousSubject,就像常规Subject一样。因此,调用 getValue() 的能力不会沿着链向下传递。这是社区的决定。我同意(和其他一些人一样),如果能够在管道之后获取值,那就太好了,但遗憾的是,这不受支持。
所以你需要做类似的事情:
const source$ = new BehaviorSubject(value);
const published$ = new BehaviorSubject(value);
const subSource$ = source$.pipe(...operators, multicast(published$));
Run Code Online (Sandbox Code Playgroud)
然后,您可以在 Publication$ 上调用 getValue() 以在该值通过运算符后检索该值。
请注意,您需要在 subSource$ 上调用 connect() (这将使其成为“热门”可观察对象)或使用 refCount()。
也就是说,这并不是真正最 Rxjs 风格的做事方式。因此,除非您有特定原因在值通过运算符后动态检索该值,而不是仅在流中订阅它,否则可能需要重新考虑该方法?
归档时间: |
|
查看次数: |
1227 次 |
最近记录: |