我一直在Angular 8中实施一个项目。在此项目中,需要具有包含不同导航栏的管理面板。要进入管理面板,您必须先登录。登录后,Navbar
应该动态更改为admin导航栏-更改应基于登录结果中的布尔值。为此,我尝试使用BehaviorSubject
,但是由于某些原因,始终可见错误值。好像BehaviorSubject
的默认值(false)尚未更新
服务:
public isResults: BehaviorSubject<boolean> = new
BehaviorSubject<boolean>(false);
public _isResults$: Observable<boolean> =
this.isResults.asObservable();
登录服务部分:
yes:boolean = true;
no:boolean = false;
login(user:User){
if (user.username != null && this.password != null) {
localStorage.setItem('currentStatus',
JSON.stringify(this.yes));
this.currentStatus =
JSON.parse(localStorage.getItem('currentStatus'));
this.isResults.next(this.currentStatus); // updating the BehaviorSubject.
// BehaviorSubject should be updated with the boolean value taken from
// the localStorage (in the localStorage the value is as it should be
// but the BehaviorSubject isn't updated, when the value from
// localStorge is assigned to it)
AppComponent。在AppComponent中,我尝试订阅BehaviorSubject
来检查值:
this.employeeService._isResults$.subscribe(
value => {
console.log(value);
}
)
value
是false
;
我可以要求您提供上述帮助吗? 谢谢,