Oblesable.create((subscriber)=> {...})中的订户在角度2中的类型

时间:2015-11-18 03:01:17

标签: typescript angular rxjs

我一直在拔头发。有没有人想出下面的Angular 2打字稿代码块中订阅者的类型?

let obs: Observable<string> = Observable.create((subscriber) => { ... }) 

2 个答案:

答案 0 :(得分:8)

创建用于创建具有自定义订阅行为的Observables

传递给Observable.create方法的函数定义了Observable订阅时应该发生的行为。因此,传入的subscriber将是实现Observer<T>接口的对象。

例如,以下代码将创建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

请注意,这实际上不会执行此代码,而是定义当新订阅者通过调用订阅时将遵循的行为 obs.subscribe(subscriber) 或者有一个功能: obs.subscribe((x) => console.log(x);

在许多情况下,不需要使用create,因为大多数常见事件排放源都有包装,所以你没有

答案 1 :(得分:1)

  

有没有人想出订户在下面的Angular 2打字稿代码块中的类型

只要next值可用,它就是onNext值。请参阅:http://reactivex.io/documentation/operators/create.html (subscriber)=>是函数(next)=>。我个人称之为[...] Application.ScreenUpdating = False Application.DisplayStatusBar = True Dim i As Integer For i = 2 To Sheets.Count ' example from my macro but you could use any repeating process Worksheets(i).Activate Application.StatusBar = "Updating Filings for " & ( i - 1 ) & " of " & (Sheets.Count - 1) & " | " & Format (( i - 1 )) / (Sheets.Count - 1), "0%") & "Complete" Next i Application.StatusBar = "" Application.DisplayStatusBar = False [...]