使用 rxjs5 获取 BehaviorSubject 当前值的简单方法 [英] Simple way to get the current value of a BehaviorSubject with rxjs5

查看:19
本文介绍了使用 rxjs5 获取 BehaviorSubject 当前值的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前在 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();

为什么 getValue()rxjs5 中被删除了,这个问题最干净的解决方案是什么?

Why getValue() was removed in rxjs5 and what's the cleanest solution to this problem?

推荐答案

正如 artur grzesiak 在评论中所指出的,BehaviorSubject 接口被清理,并且getter 现在只是 myBehaviorSubject.value.

As was pointed out by artur grzesiak in the comments, the BehaviorSubject interface was cleaned up, and the getter is now just myBehaviorSubject.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 当前值的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆