rxjs中的Observable和Subject之间有什么区别? [英] What is the difference between Observable and a Subject in rxjs?

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

问题描述

我正在浏览博客并阅读有关Observable的内容,但无法理解找出可观察对象和主题之间的区别.

I was going through this blog and reading about Observables and couldn't 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 => ...)

或者,我们可以使用 Subject 来实现 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之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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