使用rxjs5获取BehaviorSubject当前值的简单方法

编程入门 行业动态 更新时间:2024-10-24 06:26:47
本文介绍了使用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();

为什么在 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当前值的简单方法

本文发布于:2023-11-24 19:53:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简单   方法   BehaviorSubject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!