typescript Observable.create((subscriber) => { ... }) in angular 2 中的订阅者是什么类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33771080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What type is the subscriber in Observable.create((subscriber) => { ... }) in angular 2
提问by Zorthgo
I've been pulling my hair out on this one. Has anyone figured out what type the subscriber is in the Angular 2 typescript code block below?
我一直在为这个拔头发。有没有人弄清楚订阅者在下面的 Angular 2 打字稿代码块中是什么类型的?
let obs: Observable<string> = Observable.create((subscriber) => { ... })
回答by paulpdaniels
Create is for creating Observables
with custom subscription behaviors.
Create 用于创建Observables
自定义订阅行为。
The function that you pass to the Observable.create
method defines the behavior that should occur when the Observable is subscribed to. Thus, the subscriber
that is passed in would be an object that implements the Observer<T>
interface.
您传递给该Observable.create
方法的函数定义了订阅 Observable 时应该发生的行为。因此,subscriber
传入的 将是一个实现Observer<T>
接口的对象。
For instance, the following code would create an Observable
that when subscribed to, would emit two values and then complete (apologies in advance for any syntax errors, I don't use TypeScript):
例如,以下代码将创建一个Observable
,在订阅时会发出两个值然后完成(提前为任何语法错误道歉,我不使用 TypeScript):
let obs: Observable<string> = Observable.create((subscriber) => {
subscriber.next("Hello");
subscriber.next("World!");
subscriber.complete();
});
//Here is a subscriber that we define to subscribe to the Observable
let sub: Subscriber<string> = Subscriber.create(
(x) => console.log(x),
null,
() => console.log("Done"));
//At this point the method you passed to Observable.create will be invoked
obs.subscribe(sub);
//Output:
//Hello
//World!
//Done
Note that this does not actually executethis code, instead you are defining behavior that will be followed when a new Subscriber subscribes by calling
obs.subscribe(subscriber)
Or with a function:
obs.subscribe((x) => console.log(x);
请注意,这实际上并没有执行此代码,而是定义了当新订阅者通过obs.subscribe(subscriber)
使用函数调用Or订阅时将遵循的行为
:
obs.subscribe((x) => console.log(x);
In many cases the use of create
is not necessary as there are wrappers for most of the common event emission sources so you don't have
在许多情况下,create
没有必要使用 ,因为大多数常见事件发射源都有包装器,因此您没有
回答by basarat
Has anyone figured out what type the subscriber is in the Angular 2 typescript code block below
有没有人在下面的 Angular 2 打字稿代码块中找出订阅者的类型
It is the next
value whenever it becomes available. See : http://reactivex.io/documentation/operators/create.htmlonNext
is the function (subscriber)=>
. Personally I would call it (next)=>
next
只要它可用,它就是价值。参见:http: //reactivex.io/documentation/operators/create.htmlonNext
是函数(subscriber)=>
。我个人会称之为(next)=>