我正在尝试获取用户的登录名,以将其放在导航栏component
中。
我首先仅添加了没有eventManager
的行,但是如果不刷新网页就无法看到用户名,因此我决定使用以下方法,但是我不确定如何查看广播。
因此,我决定使用jhipster
构建事件管理器。我创建了broadcast
和一个subscribe
。
有更好的方法吗?
this.eventManager.subscribe('userLoggedIn', () => {
console.log('userLoggedIn called');
this.accountService.identity().subscribe(account => {
this.loggedinAccount = account.login;
});
});
答案 0 :(得分:2)
默认的JHipster模板实际上在home组件(登录页面)上显示帐户登录。我认为,如果像他们一样使用current_time - $_SESSION['clicked_time']
会更容易。
在getAuthenticationState()
中需要进行的更改才能检索身份验证状态:
navbar.component.ts
在import { Account } from 'app/core/user/account.model';
// ...
export class NavbarComponent implements OnInit, OnDestroy {
// ...
account: Account | null = null;
authSubscription?: Subscription;
// constructor
ngOnInit(): void {
// ...
this.authSubscription = this.accountService.getAuthenticationState().subscribe(
account => (this.account = account)
);
}
ngOnDestroy(): void {
if (this.authSubscription) {
this.authSubscription.unsubscribe();
}
}
中,您可以根据需要使用navbar.component.html
,例如:
account.login
答案 1 :(得分:1)
您可以使用广播订阅,但可以使用以下内容:
loggedinAccount = 'Login';
//...
ngOnInit(): {
//...
this.eventManager.subscribe('authenticationSuccess', () => {
this.accountService.identity().subscribe(account => {
this.loggedinAccount = account.login;
});
});
}
authenticationSuccess已经从JHipster在user-management.json中定义。 因此,只需使用它即可。
以HTML
<span *ngIf="isAuthenticated()">
<fa-icon icon="user"></fa-icon>
<span>{{loggedinAccount}}</span>
</span>