由于localStorage变量的值而导致动态更改导航栏的问题

时间:2019-10-20 19:09:17

标签: angular

我一直在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);
    }
  )

valuefalse

我可以要求您提供上述帮助吗? 谢谢,

0 个答案:

没有答案