[英]Angular BehaviorSubject Subscribe. Call method in component after user login and logout
I have an angular layout with the header and footer. 我的页眉和页脚有一个角度布局。 I want to update the loading of the navigation menus based on the user's role like readonly or analyst.
我想根据用户角色(如只读或分析人员)更新导航菜单的加载。 I saw a post here on SO and copied the
BehaviorSubject
code for the header component to subscribe to a boolean change in a service. 我在SO上看到了一篇文章,并复制了Header组件的
BehaviorSubject
代码以订阅服务中的布尔更改。 This works but I only got the one login button to change to the sign out button. 这可行,但是我只有一个登录按钮才能更改为退出按钮。 In my header component ts code, I have a method that will load the menus based on the user's role.
在我的标题ts代码中,我有一个方法可以根据用户的角色加载菜单。 It take a list of the menu path and load only the menu items that is appropriate for the user's role.
它获取菜单路径的列表,并仅加载适合用户角色的菜单项。 So, in addition to using
*ngIf
to changing hiding base on the subscribed boolean value change, is there a way to call a method from the subscribed change when the header component is notified by the emitted change? 因此,除了使用
*ngIf
来基于订阅的布尔值更改来更改隐藏之外,当通过发出的更改通知标头组件时,是否还有一种方法可以从订阅的更改中调用方法?
Any help is appreciated. 任何帮助表示赞赏。 Thanks.
谢谢。
Here is my header component ts code so far: 到目前为止,这是我的标头组件ts代码:
export const ROUTES: RouteInfo[] = [
{ path: '/Home', title: 'Home', icon: '', class: '', role: 'all'},
// admin
{ path: '/change-role', title: 'Change Role', icon: '', class: '', role: 'admin'},
{ path: '/users', title: 'User', icon: '', class: '', role: 'admin'},
{ path: '/products', title: 'products', icon: '', class: '', role: 'admin'},
{ path: '/admin', title: 'Admin', icon: '', class: '', role: 'admin'},
];
constructor(private dataSharingService: DataSharingService, private loginService: LoginService, private router : Router) {
this.dataSharingService.isUserLoggedIn.subscribe( value => { this.isUserLoggedIn = value })
}
ngOnInit() {
//this.getLoginStatus();
this.loadMenus();
}
loadMenus()
{
var role = this.loginService.getUserRole();
if (role == 'admin') {
this.menuItems = ROUTES.filter(menuItem => menuItem.role == 'admin');
}
else if (role == 'analyst' || role == 'reviewer') {
this.menuItems = ROUTES.filter(menuItem => menuItem.role == 'reviewer');
}
else if (role == ' readOnly') {
this.menuItems = ROUTES.filter(menuItem => menuItem.role == 'readOnly');
}
}
}
I guess this is what you want to do. 我想这就是您想要做的。
this.dataSharingService.isUserLoggedIn.subscribe(value => {
this.isUserLoggedIn = value;
this.loadMenus();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.