Angular中的Subject vs BehaviorSubject vs ReplaySubject [英] Subject vs BehaviorSubject vs ReplaySubject in Angular

查看:737
本文介绍了Angular中的Subject vs BehaviorSubject vs ReplaySubject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找那些3:

主题行为主题重播主题。我想使用它们,知道何时以及为什么,使用它们有什么好处,虽然我已经阅读了文档,观看了教程并搜索了谷歌但我没有理解这一点。

Subject, Behavior subject and Replay subject. I would like to use them and know when and why, what are the benefits of using them and although I've read the documentation, watched tutorials and searched google I've failed to make any sense of this.

那么他们的目的是什么?一个真实世界的案例将是最受欢迎的,它甚至不需要代码。

So what are their purpose? A real-world case would be most appreciated it does not have to even code.

我希望一个干净的解释不只是a + b => c你订阅了to ....

I would prefer a clean explanation not just "a+b => c you are subscribed to ...."

谢谢

推荐答案

真的归结为行为和语义。使用

It really comes down to behavior and semantics. With a


  • 主题 - 订阅者只会获得已发布的值订阅后发出。问问自己,这就是你想要的吗?订户是否需要了解以前的值?如果没有,那么你可以使用它,否则选择其中一个。例如,使用组件到组件的通信。假设您有一个组件,可以通过单击按钮发布其他组件的事件。您可以使用与主题进行通信的服务。

  • Subject - a subscriber will only get published values that were emitted after the subscription. Ask yourself, is that what you want? Does the subscriber need to know anything about previous values? If not, then you can use this, otherwise choose one of the others. For example, with component-to-component communication. Say you have a component that publishes events for other components on a button click. You can use a service with a subject to communicate.

BehaviorSubject - 缓存最后一个值。订阅者将在初始订阅时获得最新价值。此主题的语义是表示随时间变化的值。例如,登录用户。初始用户可能是匿名用户。但是一旦用户登录,则新值是经过身份验证的用户状态。

BehaviorSubject - the last value is cached. An subscriber will get the latest value upon initial subscription. The semantics for this subject is to represent a value that changes over time. For example a logged in user. The initial user might be an anonymous user. But once a user logs in, then the new value is the authenticated user state.

BehaviorSubject 已初始化为初始值。这有时对编码偏好很重要。比如说你用 null 初始化它。然后在您的订阅中,您需要进行空检查。也许没问题,或者可能很烦人。

The BehaviorSubject is initialized with an initial value. This is sometimes important to coding preference. Say for instance you initialize it with a null. Then in your subscription, you need to do a null check. Maybe OK, or maybe annoying.

ReplaySubject - 它可以缓存到指定数量的排放。任何订阅者都将在订阅时获得所有缓存的值。你什么时候需要这种行为?老实说,我不需要这样的行为,除了以下情况:

ReplaySubject - it can cache up to a specified number of emissions. Any subscribers will get all the cached values upon subscription. When would you need this behavior? Honestly I have not had need for such behavior, except for the following case:

如果你初始化一个 ReplaySubject 缓冲区大小 1 ,然后它实际上行为就像 BehaviorSubject 一样。最后一个值始终被缓存,因此它的作用就像一个随时间变化的值。有了这个,就不需要 null 检查,就像使用<$ c初始化的 BehaviorSubject 一样$ c> null ,因为在第一次发布之前,没有任何值发送给订阅者。

If you initialize a ReplaySubject with a buffer size of 1, then it actually behaves just like a BehaviorSubject. The last value is always cached, so it acts like a value changing over time. With this, there is no need for a null check like in the case of the BehaviorSubject initialized with a null, because no value is ever emitted to the subscriber until the first publishing.

所以它真正降低了你期望的行为,至于使用哪一种行为。大多数情况下,您可能希望使用 BehaviorSubject ,因为您真正想要表示的是随时间变化的值语义。但我个人认为用 1 初始化的 ReplaySubject 的替换没有任何问题。

So it really comes down the behavior you are expecting, as for which one to use. Most the time you will probably want to use a BehaviorSubject because what you really want to represent is that "value over time" semantic. But I personally don't see anything wrong with the substitution of ReplaySubject initialized with 1.

当你真正需要的时候,你想要避免使用香草主题是一些缓存行为。例如,您正在编写路由警卫或解决方案。您获取该守卫中的一些数据并将其设置在服务主题中。然后在路由组件中,您订阅服务主题以尝试获取在防护中发出的值。哎呀。价值在哪里?它已经被释放了,DUH。使用缓存主题!

What you want to avoid is using the vanilla Subject when what you really need is some caching behavior. Take for example you are writing a routing guard or a resolve. You fetch some data in that guard and set it in a service Subject. Then in the routed component you subscribe to the service subject to try to get that value that was emitted in the guard. OOPs. where's the value? It was already emitted, DUH. Use a "caching" subject!

  • What are RxJS Subject's and the benifits of using them?

这篇关于Angular中的Subject vs BehaviorSubject vs ReplaySubject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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