簡體   English   中英

只運行一次 observables,然后檢查是否完成 - angular2 typescript

[英]Only run observables once, then check if completed after - angular2 typescript

Observables非常新,所以很抱歉,如果這是基本的,但我嘗試過谷歌搜索,但似乎無法找到我想要的。

這是一個示例 plunkr,非常基本。
https://plnkr.co/edit/kgECPQyoKqY7RamebUUu?p=preview

為什么我的Initialized方法直到我的服務調用它才被調用? 我認為initialized observable 會在我執行Observable.forkJoin立即執行? 我需要以某種方式開始這個過程嗎?

為什么我的myService.loadOtherData方法似乎永遠不會調用 subscribe 方法???

提前致謝

如果您對 Observables 非常陌生,我建議您從一個基本示例開始。

例如:(偽代碼,也許)

let observable = new Observable<string>(observer => {
  if (1 == 1) {
    observer.next("one is one");
  } else {
    observer.error("one is not one");
  }
});
observable.subscribe(
  response => console.log(response), 
  error -> console.log(error)
);

使用主題更相似,但主題可以更自由地使用:

let subject = new Subject();
subject.subscribe(response -> console.log(response));
subject.next('hello');

我相信我看到您將兩者結合起來,訂閱主題然后使用可觀察對象獲取數據? 有了這些例子,你有足夠的信息還是需要一個非常具體的例子?

編輯:

你的this.initialized.subscribe '開始這個過程'。 您太晚調用 loadOtherData 函數,這使您無法在數據返回時訂閱 this.initialized。 這就是為什么你看不到 console.log('initialized')

編輯2:

請注意BehaviorSubject保存它的數據並在訂閱時返回它。 不需要用空字符串初始化它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM