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

查看:29
本文介绍了subscribe 有什么作用,它与 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.

Observable 有两种:冷的和热的.

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

  • Cold Observables:这些 Observables 在您订阅之前不会发出数据,基本上,数据在您请求之前不存在(例如 Ajax 请求).
  • Hot Observables :这些对象开始发射而不关心是否有订阅者在等待数据.
  • 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?你说只有两种 Observable .你是个骗子!

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

不是真的;ConnectableObservables 是 Observables,一旦你调用它们的 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()).

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

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