如何获取 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章