什么是 observable、observer 和 subscribe 的 angular? [英] What is observable, observer and subscribe in angular?
问题描述
我正在学习 angular,但我对这些可观察的、观察者和订阅感到困惑事物.所以请解释一下.
I am learning angular and i got confuse in these observable, observer and subscribe thing. So please explain.
推荐答案
这是一个简单的视觉效果,可以看出区别:
Here is a simple visual to see the difference:
如上所示……Observable 是事件或数据流.它们通常从 Angular 方法返回,例如 http.get
和 myinputBox.valueChanges
.
As seen above ... an Observable is a stream of events or data. They are often returned from Angular methods, such as the http.get
and the myinputBox.valueChanges
.
订阅启动"可观察流.如果没有订阅(或异步管道),流将不会开始发出值.这类似于订阅报纸或杂志……除非您订阅,否则您不会开始获得它们.
Subscribing "kicks off" the observable stream. Without a subscribe (or an async pipe) the stream won't start emitting values. It's similar to subscribing to a newspaper or magazine ... you won't start getting them until you subscribe.
subscribe 方法接受一个观察者.观察者有三种方法:
The subscribe method takes in an observer. An observer has three methods:
每次从 observable 发出项目时处理的方法.
The method to process each time an item is emitted from the observable.
处理发生的任何错误的方法.
The method to process any error that occurs.
观察者完成时清理任何东西的方法.在处理 Angular 的 observables 时,很少使用最后一个.
The method to clean up anything when the observer completes. This last one is seldom used when working with Angular's observables.
希望这会有所帮助.
(我同意……试图通过文档的树木看到森林是一个相当大的挑战.我知道他们正在努力改进它们.)
(And I agree ... trying to see the forest through the trees of the docs is quite a challenge. I understand they are working to improve them.)
这篇关于什么是 observable、observer 和 subscribe 的 angular?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!