使用 Subject 和 Observable 之间有什么区别,分别有什么用途? [英] What are the differences between using a Subject and an Observable, and what are the uses for each?

查看:47
本文介绍了使用 Subject 和 Observable 之间有什么区别,分别有什么用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学会了两种不同的方式来制作 Observable.第一个是一个主题,就像这样:

I have learned of two different ways of making an Observable. First one was with a Subject, like so:

// file A
const message$ = new Subject();

// file B
message$.subscribe( (message) => console.log(message) );

// file C
message$.next("Hello there!");

这种创建 Observable 的方法让我有办法将数据从文件 B 交换到文件 C.

This method of creating an Observable allows me to have a way to exchange data from file B to file C.

第二种方式是通过 Observable 类,如下所示:

The second way is via the Observable class, like so:

// file A
const click$ = new Observable( function(observer) { 
  //Alternatively, I can use Observable.create()
  document.addEventListener('click', (e) => observer.next(e));
});

// file B
click$.subscribe( (cl) => console.log(cl) );

我可以收集到的主题方式和 Observable 方式之间的主要区别是,我不确定如何在某个文件 C 与 Observable 的订阅者之间进行某种通信.基本上,click$ 没有 .next() 方法,观察者方法在我们传递给 observable 的函数中.

The main difference that I can gather between the Subject way and the Observable way is that I am not sure how to have some sort of communication between some file C, to the subscribers of the Observable. Basically, click$ does not have a .next() method, and the observer methods are in the function that we pass to the observable.

除了这种行为上的差异之外,使用 Subject 制作的 observable 和使用 Observable 制作的可观察对象之间还有另一个区别吗

Other than this difference in behavior, is there another difference between observables made with Subject, and those made with Observable

推荐答案

A Subject 同时是 ObservableObserver.这使得它非常易于使用,因为您可以获得对 Observer 的引用,您可以在代码中传递它并从您想要的任何地方发出项目.但是,当您从 Observable 的声明性定义切换到命令式定义时,这会大大增加代码的错误倾向.

A Subject is both Observable and Observer at the same time. That makes it so tempting to use, because you get a reference to an Observer that you can pass around in your code and emit items from wherever you want. However, that increases the error-proneness of your code by a lot, as you switch from a declarative definition of your Observable to a imperative one.

一般来说,你应该使用 Observable 创建函数(of, 来自, create) 尽可能.我会说大多数情况可以在没有主题的情况下解决.但是,有一个陡峭的学习曲线,因为您必须了解大多数 Observable 创建函数才能遵循该模式.

Generally speaking, you should use Observable creation functions (of, from, create) wherever possible. I'd say most cases can be solved without Subjects. There is a steep learning-curve though, as you must get to know most of the Observable creation functions in order to follow that pattern.

Subject 对于习惯于命令式编码(即:使用 JS 之类的脚本语言)的开发人员来说可能更自然,因为它有点类似于回调函数的简单包装对象.有人可能会问,如果它不受欢迎,为什么还要有一个主题.

Subject might come more natural to developers who are used to code imperatively (that is: with a script language like JS), as it kind of resembles a simple wrapper object for a callback function. And one might ask, why is there a Subject anyway if its not desirable.

根据此文章,主语只用于一种情况:

According to this article, Subjects are to be used in one case only:

在没有任何直接外部来源的情况下,强制且有状态地生成热可观察对象.

To generate a hot observable imperatively and statefully, without any direct external source.

简而言之,这意味着:当您没有任何外部源(如 Observable、Promise 或 Event)并且需要从函数内部多播类的状态时,请使用 Subject.不过,你不应该把那个主题暴露给别人!

In short, that means: Use Subject when you don't have any external source (like an Observable, Promise or Event) and need to multicast the state of a class from inside a function. You shouldn't expose that Subject to others, though!

我建议给你读这篇文章,它会让事情变得清晰.

I suggest reading this article to you, it will clear up things.

这篇关于使用 Subject 和 Observable 之间有什么区别,分别有什么用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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