以前,在 rxjs4 中,BehaviorSubject中有一个方法称为: getValue()(请点击此处).
Previously in rxjs4 there was a method in the BehaviorSubject called: getValue() (doc here).
rxjs5 中不再存在此方法.
因此,我发现获得BehaviorSubject值的唯一解决方案是:
So the only solution that I found to get the value of a BehaviorSubject was:
let value; myBehaviorSubject.take(1).subscribe( (e) => value = e );此代码同步运行(我不完全理解为什么,但是它确实...)并获取值.它可以工作,但是它不像存在 getValue()时那样干净:
This code runs synchronously (I do not exactly understand why, but it does ...) and gets the value. It works, but it's not as clean as it could be if getValue() was present:
let value = myBehaviorSubject.getValue();为什么在 rxjs5 中删除了 getValue(),最干净的解决方案是什么?
Why getValue() was removed in rxjs5 and what's the cleanest solution to this problem?
推荐答案正如 artur grzesiak 在评论中指出的那样, BehaviorSubject 接口已被清理,并且现在,吸气剂就是 .value .
As was pointed out by artur grzesiak in the comments, the BehaviorSubject interface was cleaned up, and the getter is now just .value.
我只是想将其添加为答案,因为我几乎没有阅读原始问题的注释,并且会错过正确的答案.
I just wanted to add this as an answer because I almost didn't read the comments to the original question, and would have missed the correct answer.
更多推荐
使用rxjs5获取BehaviorSubject当前值的简单方法
发布评论