Angular 中的主题 vs 行为主题 vs ReplaySubject [英] Subject vs BehaviorSubject vs ReplaySubject in Angular

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

问题描述

我一直想了解这 3 点:

I've been looking to understand those 3:

我想使用它们,并知道何时以及为什么使用它们,使用它们有什么好处,尽管我已经阅读了文档、观看了教程并在谷歌上搜索过,但我对此一无所知.

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 你订阅了...."

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 - 订阅者只会获得在订阅后 发出的已发布值.问问自己,这是你想要的吗?订阅者是否需要了解以前的值?如果没有,那么您可以使用它,否则选择其他之一.例如,组件到组件的通信.假设您有一个组件,它在单击按钮时为其他组件发布事件.您可以使用具有主题的服务进行通信.

  • 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. A 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 any need for such behavior, except for the following case:

如果你用 1 的缓冲区大小初始化一个 ReplaySubject,那么它实际上行为就像一个 BehaviorSubject>.最后一个值总是被缓存,所以它就像一个随时间变化的值.有了这个,就不需要像用 null 初始化的 BehaviorSubject 那样进行 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. In this instance, no value is ever emitted to the subscriber until the first publishing.

所以这真的归结为您期望的行为(至于使用哪个).大多数情况下,您可能想要使用 BehaviorSubject,因为您真正想要表示的是随时间变化的价值"语义.但我个人认为用 1 初始化的 ReplaySubject 替换没有任何问题.

So it really comes down to the behavior you are expecting (as for which one to use). Most of 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.

你想要避免的是使用普通的Subject,而你真正需要的是一些缓存行为.以您正在编写路由保护或解析为例.您在该守卫中获取一些数据并将其设置在服务 Subject 中.然后在路由组件中订阅服务主题以尝试获取在守卫中发出的值.面向对象.价值在哪里?它已经发出了,呃.使用缓存"主题!

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!

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

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