angular - 由于 localStorage 变量的值而动态更改导航栏的问题
问题描述
我一直在 Angular 8 中实现一个项目。在这个项目中,需要有一个包含不同导航栏的管理面板。要进入管理面板,您必须登录。登录后,Navbar
应将其动态更改为管理导航栏-更改应基于登录结果中的布尔值。为此,我尝试使用BehaviorSubject
,但由于某些原因,始终可见 false 值。看起来好像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 我尝试订阅以BehaviorSubject
检查值:
this.employeeService._isResults$.subscribe(
value => {
console.log(value);
}
)
value
是false
;_
我可以请你帮忙吗?谢谢,
解决方案
推荐阅读
- orocrm - 不支持 filter[website1] 中的运算符 ['~','!~']
- react-native - 排毒 - 通过 WebView 登录屏幕
- laravel - 试图获取非对象的属性“部门”
- javascript - 意外的标识符 [object Object]
- c# - WPF SignalR 服务器
- machine-learning - 强化学习:Actor-Critic 总是比策略梯度方法更好吗?
- asp.net-mvc - 如何通过 netcore3.1 中的环境变量更改 Kestrel (AspNetCore) 侦听端口
- python-3.x - 如何告诉`ipywidgets.interactive`,它应该只考虑特定参数作为小部件?
- c++ - 不按特定顺序跳过多个循环(C++)
- azure - 如何结合 AD B2C(MSAL) 和 CosmosDB