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

问题描述

我正在浏览这个并阅读有关Observables的内容,我无法弄清楚Observable与主题之间的区别

i was going through this blog and to read about Observables and i couldnt figure out the difference between the Observable and a Subject

推荐答案

在流编程中有两个主要界面:可观察观察者

In stream programming there are two main interfaces: Observable and Observer.

可观察适用于消费者,可以进行转换和订阅:

Observable is for the consumer, it can be transformed and subscribed:

observable.map(x => ...).filter(x => ...).subscribe(x => ...)

观察者是用于提供可观察量的接口来源:

Observer is the interface which is used to feed an observable source:

observer.next(newItem)

我们可以使用观察者创建新的可观察

We can create new Observable with an Observer:

var observable = Observable.create(observer => {
    observer.next('first');
    observer.next('second');
    ...
});
observable.map(x => ...).filter(x => ...).subscribe(x => ...)

或者,我们可以使用主题来实现 Observable Observer 接口:

Or, we can use a Subject which implements both the Observable and the Observer interfaces:

var source = new Subject();
source.map(x => ...).filter(x => ...).subscribe(x => ...)
source.next('first')
source.next('second')

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

09-02 03:29