0
我的代碼是這樣Angular2中的BehaviorSubject不是第二次設置值?
login.component.ts
loginSucess() {
this.MenuService.setUserName(userauthresponce.username);
}
MenuService.ts
private usernameSource = new BehaviorSubject<any>(0);
setUserName (username) {
this.usernameSource.next(username);
}
getUserName() {
return this.usernameSource.asObservable();
}
other.component.ts
constructor(MenuService:MenuService) {
this.MenuService.getUserName().subscribe(responce => {
console.log('username', responce);
this.username = responce;
});
}
/* it's not working hear*/
switchrole() {
this.router.navigate(['/dashboard']);
this.MenuService.setUserName(this.username);
}
我在第一次登錄時獲取用戶名,之後當用戶單擊切換角色按鈕時它正在調用切換角色功能,在此功能中,我必須將該頁導航到儀表板,在這種情況下,我將用戶名作爲0;
即使我設置爲從switchrole()的用戶名,但是這是不工作
,我沒有得到我的錯誤,請幫我
你在哪裏提供'MenuService'? – yurzui