问题:
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