订阅有什么作用,它与Observable有何关系? [英] What does subscribe do, and how it is related to Observable?

查看:90
本文介绍了订阅有什么作用,它与Observable有何关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular的新手,我遵循的教程中有"Observable"一词.导师解释了这一点,但我并不完全理解.

I'm new to Angular and the tutorial I followed has the term "Observable". The tutor explained it, but I didn't completely understand.

什么是Observable,为什么我们总是必须调用observable.subscribe()?

What is an Observable, and why do we always have to call observable.subscribe()?

subscribe()实际做什么?

推荐答案

什么是Observable?

一个Observable可以看作是一个数据源.该数据可能存在(或不存在),并且可能随时间变化(或不随时间变化).

What is an Observable?

An Observable can be seen as a data source. That data might exist (or not) and might change over time (or not).

一个Observable会发射数据,直到不再有任何要发射的数据然后完成(有一些Observable永远不会完成)或引发异常(错误处理是Observable组合的重要组成部分).

An Observable emits data, until it has nothing to emit anymore and then completes (there are some Observable that will never complete) or throws an exception (error handling is a big part of Observable combination).

您可以使用mapmergeswitchMap等运算符组合这些数据源或更改发出的数据.因此,一个数据源可以是另一个数据源的组合或组合许多其他人中.

You can combine these data-sources or alter the emitted data using operators like map, merge, switchMap, etc. So, a data-source can be an alteration of another data-source or the combination of many others.

正如我所说,Observable是源,如果要使用该源中的数据,则需要subscribe()Observable,然后您会收到发出的任何数据的通知.

As I said, an Observable is a source, If you want to use the data from that source, you need to subscribe() to the Observable and then you get notified of any data emitted.

有两种观测值:冷观测值和热观测值.

There are two kind of Observables: cold and hot ones.

  • 冷可观测对象:那些可观测对象在您订阅它们之前不会发出数据,基本上,直到您要求数据(例如Ajax请求)时,数据才不存在.
  • 热点可观测对象:如果有订阅者正在等待数据,这些对象将开始发射而无需关心.
  • Cold Observables: Those are Observables that do not emit data until you subscribe to them, basically, data does not exists until you ask for it (e.g. Ajax requests).
  • Hot Observables : These ones start emitting without caring if there is or not a subscriber waiting for data.

大多数时候,您必须处理冷的Observables(AJAX请求),这就是为什么您需要订阅它们的原因,如果没有此订阅,则您仅定义数据源,然后再不触发请求.

Most of the time, you have to deal with cold Observables (AJAX requests), that's why you need to subscribe to them, without this subscription you only define a data source, and then never trigger the request.

因此,让我们考虑一下带有视频隐喻的Observable:

So let's think about Observable with a video metaphor:

  • 冷的Observable就像是一项VOD服务:在您要求时播放视频(subscribe()).
  • 热门Observable就像普通电视一样:播放视频时不会考虑任何人是否要求的事实.
  • A cold Observable is like a VOD service : Videos are broadcasted when you ask for it (subscribe()).
  • A hot Observable is like regular TV : Video are broadcasted without any regard to the fact that anyone asks for it or not.

什么? ConnectableObservable?您说过只有两种可观察的.你是骗子!

What? ConnectableObservable? You said there was only two kind of Observable. You're a liar!

并非如此; ConnectableObservableObservable,它们会在您调用它们的connect()方法后立即发出数据.换句话说,只要您调用connect()方法,此Observable就会变热.

Not really; ConnectableObservables are Observables that emit data as soon as you call their connect() method. In other words, this Observable becomes hot as soon as you call the connect() method.

您可以使用某些运算符(例如publish())将冷的Observable转换为ConnectableObservable.

You can turn a cold Observable into a ConnectableObservable using some operators (like publish()).

这篇关于订阅有什么作用,它与Observable有何关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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