問題:
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