如何獲取 RxJS Subject 或 Observable 的當前值? - How to get current value of RxJS Subject or Observable?

問題:

I have an Angular 2 service:我有一個 Angular 2 服務:

import {Storage} from './storage';
import {Injectable} from 'angular2/core';
import {Subject}    from 'rxjs/Subject';

@Injectable()
export class SessionStorage extends Storage {
  private _isLoggedInSource = new Subject<boolean>();
  isLoggedIn = this._isLoggedInSource.asObservable();
  constructor() {
    super('session');
  }
  setIsLoggedIn(value: boolean) {
    this.setItem('_isLoggedIn', value, () => {
      this._isLoggedInSource.next(value);
    });
  }
}

Everything works great.一切都很好。 But I have another component which doesn't need to subscribe, it just needs to get the current value of isLoggedIn at a certain point in time.但是我有另一個不需要訂閱的組件,它只需要在某個時間點獲取 isLoggedIn 的當前值。 How can I do this?我該怎麼做?


解決方案:

參考一: https://en.stackoom.com/question/2VcnR
參考二: https://stackoom.com/question/2VcnR
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章